Scala

6 / 53

Scala - Compiling & Running Code




Not able to play video? Try with vimeo

Let's write a "Hello world" program to learn how to compile and run a scala code.

Login to the CloudxLab web console. Create a directory 'scala' and go inside it. Create a file hello_world.scala using the command "nano hello_world.scala". You can also use vim editor instead of nano. Type the code as displayed on the screen. Press "control o" to save the file and then press enter. Press "control x" to exit the nano editor.

To compile the code, type "scalac hello_world.scala" and press enter. You can see two class files HelloWorld$.class, HelloWorld.class. These .class files are generated bytecode which gets executed by JVM.

To run the code, type "scala HelloWorld". You can see that "Hello, world!" is printed on the screen.

Let's understand the structure of the code. It consists of a method called "main". The "main" method takes the command line argument as a parameter, which is an array of strings. and prints "Hello world" on the screen. HelloWorld is a singleton object, that is a class with a single instance. We'll discuss "object" later in the course.

If we've just one .scala file, we can also run it using the scala interpreter. Type "scala hello_world.scala" and press enter. We can see that "Hello, world!" gets printed on the screen.

We can evaluate Scala code by the way of arguments using -e option. Type scala -e 'println("Hello, World!")' and "Hello, world!" gets printed on the screen


Loading comments...