Scala for Java Refugees
January 26, 2008 at 7:24 am | In scala | No CommentsTags: scala
Saw the series from
http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-1
They really got quite a tutorial!
The Explore Scala Series - Overview
January 25, 2008 at 4:28 pm | In scala | No CommentsTags: java, programming, scala
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
Features
-
Object-Oriented
-
Everything is an object
-
Numbers are objects also (have methods)
-
-
Functional
-
Every function is a value
-
Supports higher order functions
-
-
Statically typed
-
Can ineract with java directly
Everything is an object
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.
Functional Example
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
}
}
Scalable java - Scala
January 24, 2008 at 4:32 am | In java, scala | No CommentsTags: java, scala
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:
-
compiling to java bytecode(as you can see from above, meaning that it runs on the JVM (hmm… its quite interesting that it is said to be scalable when its running on JVM, but anyhow, gotta explore further)
-
functional language, yet borrows ideas from OO (definitely a very attractive feature)
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 ![]()
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.