Saw the series from
http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-1
They really got quite a tutorial!
Saw the series from
http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-1
They really got quite a tutorial!
I thought of reading up scala and come up with a simple series of scala tutorials, at the same time, help myself familarise with the language. The idea of functional language really attracts me, of course in java 1.6, there’s support for scripting language,s
As mentioned above, number is also an object, i.e they have methods and behave like what we know for a typically object. So instead of
object A{ def main(args: Array[String]){ println(1 + 1); } }
We could have
object A{ def main(args: Array[String]){ println(1.+(1)); } }
where in here + is a method of Number.
object A{ def add(num1: int, num2: int): int = { num1 + num2 } def sub(num1: int, num2: int): int = { num1 - num2 } def op(f: (int,int) => int, para1: int, para2: int): int= { f(para1,para2) } def main(args: Array[String]){ println(op(add, 1,2)) //takes in an add function and 2 para println(op(sub, 1,2)) //takes in a sub function and 2 para } }
Hello World in java:
public class HelloWorld { public static void main(String args[]){ System.out.println("Hello World!"); } }
Hello World in scala:
object HelloWorld{ def main(args: Array[String]){ println("Hello World!") } }
Into the details(decompiled form):Java:
import java.io.PrintStream; public class HelloWorld { public HelloWorld() { } public static void main(String args[]) { System.out.println("Hello World!"); } }
Scala:
public final class HelloWorld { public static final int $tag() { return HelloWorld$.MODULE$.$tag(); } public static final void main(String args[]) { HelloWorld$.MODULE$.main(args); } } //-------------------------------------- import scala.Predef$; import scala.ScalaObject; public final class HelloWorld$ implements ScalaObject { public HelloWorld$() { } public void main(String args[]) { Predef$.MODULE$.println("Hello World!"); } public int $tag() { return scala.ScalaObject.class.$tag(this); } public static final HelloWorld$ MODULE$ = this; static { new HelloWorld$(); } }
Seems like there’s more stuff for a simple example like this.
But the language seems quite cool.
Several powerful factors includes:
I’m still quite new to the language but probably I will compare (syntax-wise) java with scala when I’m doing slides for my java tutorial class 🙂