Wednesday, December 25, 2013

Just For Fun - Hello World in Scala

Prologue
I just assume that you already have Scala installed in your Windows computer. Once you have installed it, you can open a command prompt window (simply by press "windows"+R (Run), then type "cmd" enter OK). In the command prompt window, you can enter a simple command to open Scala interactive shell:


If you get the above result (note: the version number may be different), then everything is fine. But you can also have the misfortune to get an error message, that the command is not recognized. If that happen, please remember to setup user environment correctly. As example in my case:


Hello World in Console
 object HelloWorld {  
  def main(args: Array[String]) {  
   print("Enter your name:")  
      val name = readLine()    
   if (name != null) println("Hello " + name + "!")  
  }  
 }  

compile it!
$> scalac HelloWorld.scala
By default scalac generates the class files into the current working directory. You may specify a different output directory using the -d option.

Execute it!
$> scala HelloWorld
scala allows us to specify command options, such as the -classpath (or -cp)

And the result:





This is just for fun. I'm more interested to use scala in web projects, as example by using play framework.

No comments:

Post a Comment