Browse files

first commit

initial examples on java integration
  • Loading branch information...
0 parents commit fc56de231129fb335854e56bce2cce5fd00c4116 @fernandezpablo85 committed Oct 9, 2011
8 .gitignore
@@ -0,0 +1,8 @@
+# binaries
+target
+
+# sbt files
+project/boot/
+
+# idea files
+.idea*
20 README.markdown
@@ -0,0 +1,20 @@
+7 Reasons Why You Should Give Scala a Try Now
+===============================================
+
+By now you probably have heard about scala, it's the functionall-oop language that big guys like Twitter or LinkedIn are using lately. Perhaps you're still sceptic about this, and think that this is "Yet Another Language", that the hype will not last.
+
+Also, everyone seems to be talking about some particular features of Scala, like actors, that look nice, sound nice but you realize you're never gonna use them anyway.
+
+This guide tries to answer a simple question: "What does scala bring to the table, right now, to my existing java applications?".
+
+I'm going to talk about the features *I* consider important, so this guide might be biased. Hopefully someone else will find this useful to give scala a shot, or to convince his/her boss to do it.
+
+I've separated it on 7 reasons (this number might vary):
+
+ 1. Java Integration
+ 2. Better Separation of Concerns with Traits [in progress]
+ 3. Type Inference (where is it helpful?) [in progress]
+ 4. Functions are "first class citizens" (What does this mean? and why does it matter?) [in progress]
+ 5. Good abstractions over concurrency [in progress]
+ 6. Sbt (Simple Build Tool)[in progress]
+ 7. Great Community [in progress]
12 build.sbt
@@ -0,0 +1,12 @@
+
+
+name := "scala-reasons"
+
+
+organization := "com.example.scala"
+
+
+version := "0.1"
+
+
+scalaVersion := "2.8.1"
23 src/main/scala/java/org/example/scala/reason1/types/JavaClass.java
@@ -0,0 +1,23 @@
+package org.example.scala.reason1.types;
+
+public class JavaClass
+{
+ private int nativeInt;
+ private int integer;
+ private String string;
+
+ public void setNativeInteger(int nativeInt)
+ {
+ this.nativeInt = nativeInt;
+ }
+
+ public void setInteger(Integer integer)
+ {
+ this.integer = integer;
+ }
+
+ public void setString(String string)
+ {
+ this.string = string;
+ }
+}
6 src/main/scala/org/example/scala/reason1/types/JavaIntegration.scala
@@ -0,0 +1,6 @@
+package org.example.scala.reason1.types
+
+object JavaIntegration extends Application with NumberExamples with StringExamples
+{
+
+}
32 src/main/scala/org/example/scala/reason1/types/NumberExamples.scala
@@ -0,0 +1,32 @@
+package org.example.scala.reason1.types
+
+trait NumberExamples
+{
+ println("=> Running NumberExamples")
+
+ // Freely instantiate java classes.
+ val numbersJava = new JavaClass
+
+ // By default, scala uses it's own types for numeric literals, like "Int".
+ val one = 1
+ assert(one.isInstanceOf[Int])
+
+ // But it coerces them to the appropiate java types if needed.
+ numbersJava.setNativeInteger(one)
+ numbersJava.setInteger(one)
+
+ // Numbers will auto convert to RichInt to use some neat extra functions.
+ // Check them out at: http://www.scala-lang.org/api/current/index.html#scala.runtime.RichInt
+
+ // Getting hex strings.
+ assert(10.toHexString == "a")
+
+ // Getting binary strings.
+ assert(2.toBinaryString == "10")
+
+ // For loops are actually iterations over a Range object, created by a 'to' method.
+ for (i <- 1 to 20)
+ {
+ // do something 20 times with 'i'.
+ }
+}
53 src/main/scala/org/example/scala/reason1/types/StringExamples.scala
@@ -0,0 +1,53 @@
+package org.example.scala.reason1.types
+
+import java.lang.{String => JavaString}
+
+trait StringExamples
+{
+ println("=> Running StringExamples")
+
+ val stringsJava = new JavaClass
+
+ // String literals are an exception. It defaults to java.lang.String.
+ val string = "hello world"
+ assert(string.isInstanceOf[JavaString])
+
+ // No need for coercion here.
+ stringsJava.setString(string)
+
+ // Scala auto converts (using implicit conversions) to StringOps to add some useful methods of it's own.
+ assert(string.reverse == "dlrow olleh")
+
+ // Note that this doesn't change the string type.
+ assert(string.isInstanceOf[JavaString])
+ assert(string.reverse.isInstanceOf[JavaString])
+ stringsJava.setString(string.reverse)
+
+ /*
+ * More great stuff available on StringOps:
+ * (check the full list at: http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.StringOps)
+ */
+
+ // Access single chars of the string with an array like syntax.
+ assert('h' == string(0))
+
+ // NOTE that it returns Characters not Strings.
+ assert("h" != string(0))
+
+ // Use diff to get the differences between two strings.
+ assert(string.diff("hello ") == "world")
+
+ // mkString is like join()
+ assert("abcd".mkString(",") == "a,b,c,d")
+
+ // Scala also supports three quote strings, that allow any character (like quotes or slashes) without escaping.
+ // They also support multiple lines.
+ val threeQuote = """this is my "long" string, it's cool uh?"""
+ val markup = """
+<div class='%s'>
+ <span>Hello my friend '%s'</span>
+</div>"""
+ // println(String.format(markup, "greet", "Pablo"))
+
+
+}

0 comments on commit fc56de2

Please sign in to comment.