Say hello to Java and Scala

Written on August 30th, 2009 by Thorno shouts

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. :)

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Live
  • Reddit
Filed under Programming Tags:, ,

Leave a Reply

(required)

(required)