Scala's Hello World can be either similar to Java's, or simpler depending on how the problem is approached. This article will approach the problem from both the Java style as well as the much more pure functional style. Remember that Scala is a highly expressive functional language. The second form of the hello world program will show this feature much more clearly.
Step 1: Create a Scala source file
Scala does not have the same restrictions on source file naming that Java has. Therefore, you can give the Scala source file pretty much any name you like, so long as it ends in .scala. The fist form of Hello World looks as:
__________________________________________________________________________________
object HelloWorld { def main(args: Array[String]) { println("Hello in scala from ecocrypt.blogspot.com") } }
__________________________________________________________________________________
Step 2: Compile the Scala Source
At a command prompt, type the scala compiler command:
prompt % prompt % scalac HelloWorld.scala prompt %
prompt % scalac HelloWorld.scala
The result of this will be to produce an output file called HelloWorld.class, this class file is a byte code version of the HelloWorld.scala source file. This class file is identical to a Java class file, but has Scala class references embedded in it.
Step 3: Running HelloWorld
At the command prompt, type:
prompt % prompt % scala HelloWorld Hello in scala from ecocrypt.blogspot.com prompt %
prompt % scala HelloWorld
Hello in scala from ecocrypt.blogspot.com
Congratulate yourself! You have successfully installed the Scala compiler and execution environment, created a simple scala source file compiled it to byte code, and run it inside of the Scala/Java Virtual Machine.
The exact same thing could be accomplished using a scala Application. The source code for a Scala application based solution would look as:
__________________________________________________________________________________
object HelloWorld2 extends Application {
println("Hello world in scala from ecocrypt.blogspot.com")
}
__________________________________________________________________________________
No comments:
Post a Comment
All comments are greatly appreciated! Comments will be moderated, and usually appear within 1 day. If you like a post, instead of saying you like it, vote for it or digg it (links provided). If there is any ERROR, PLEASE Comment I strive for good quality complete content.