Say hello to Java and Scala
Let’s write a legendary “Hello world” program in both Java and Scala language.
This is how we do in Java.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello world");
}
}and this is how we do in Scala
object Hello {
def main(args : Array[String]) = println("Hello world")
}So when you execute both code,they actually return the same result.What about printing arguments instead?
Java style
public class HelloArgument {
public static void main(String[] args) {
for (String arg : args) {
System.out.println("Hello, " + arg);
}
}
}
Scala style
object HelloArgument {
def main(args : Array[String]) = args.foreach(arg => println("Hello, " + arg))
}
Pretty impressive,huh?Comparing Java,we are actually writing less and more readable code in Scala,and the results are the same.
Happy coding.












Recent Comments