Writing A HelloWorld in Scala File
Steps :
1. Download and Install Scala from here
2. Set the scala home.
3. Open a Notepad copy the below mentioned program and save as HelloWorld.scala
// println is in the Predef
println("Hello World !")
4. Run as > scala -classpath classes 1.HelloWorld.scala
Observations:
No Main Method is required.
No need to add System.in package. The println method is imported implicitly.
No class or any Static Class Required.
We dont need to compile it.
Writing A HelloWorld Interactively
scala
This is a Scala shell.
Type in expressions to have them evaluated.
Type :help for more information.
scala> object HelloWorld {
| def main(args: Array[String]) {
| println("Hello, world!")
| }
| }
defined module HelloWorld
scala> HelloWorld.main(null)
Hello, world!
unnamed0: Unit = ()
Writing A HelloWorld in the Lines of Java Static Method
Steps :
a.Copy the below mentioned code into a file named HelloWorldStatic .
b. Run > scalac HelloWorldStatic.scala
c. Run > scala HelloWorldStatic
object HelloWorldStatic {
def main(args: Array[String]) {
println("Hello, world!")
}
}
Points to Note :
Note that the code in scenario one would work fine using scala command, but you would not be able to compile the code and you would get error some link the one mentioned below
1.HelloWorld.scala:2: error: expected class or object definition
println("Hello World !")
That’s because the scala compiler expects you to provide a full class to compile instead of script.
HelloWorld extending Application Class
Below is an example which extends Application Class.
object HelloWorldApplication extends Application {
println("Hello, World!")
}
Observations:
1. Copy the above mentioned code into a file named HelloWorldApplication.scala.
2. Run > scalac HelloWorldApplication .scala
3. Run > scala HelloWorldApplication
Please refer to the sitefor more on this.

No comments:
Post a Comment