25 September, 2014

Learning Scala- Hello World!


If you are using Ubuntu, Install Scala by running the following command on terminal:

sudo apt-get install scala

This would install Scala on your machine.

install Scala

Create a project directory lets say : MyProject and go to it

mkdir MyProject
cd MyProject/

Using terminal run the following lines of code

mkdir -p src/main/scala
This would make a project directory structure for Scala project that we are going to create

echo 'object Hi { def main(args: Array[String]) = println("Hi!") }' > src/main/scala/hw.scala

Here we are creating a file named hw.scala, inside src/main/scala direcctory which we created in previous step. and wrote the line
object Hi { def main(args: Array[String]) = println("Hi!") }
in it. Let's format it and understand
object Hi {
def main(args: Array[String]) = println("Hi!")
}
Here, we are defining an object Hi, with a main function inside.
This function simply prints a Hi! on the screen.

Lets compile our code now:
scalac src/main/scala/hw.scala
This would compile our scala code to byte code.
Test the code by running it
scala Hi






scala Hello World Test
It should print "Hi!" on console.

You just created a basic program in Scala.