As a first step into learning Scala and as one who is familiar with Java, let us compare the customary Helloworld programs in Java and Scala. You might already know that to run a Java program, there must be a public class with a main method that takes one parameter, a String[ ], and has a void return type.
also read:
For example:
package javaapplication; public class Main { public static void main(String[] args) { System.out.println("Hello World"); } }
In Scala, an equivalent program looks something as follows –
package scalaapplication object Main { def main(args: Array[String]): Unit = { println("Hello World") } }
The output of both of the above programs is to print out Hello World. We compare and contrast the first programs in Java and Scala as follows:
- Both the programs begin with a package declaration. While the package statement or rather every statement in the Java program ends with a semi-colon, semicolon is not mandatory in the Scala program. In a Scala program, the compiler does not care whether a statement ends with a semicolon or not. Newline marks end of a statement and beginning of another. Semicolon is required though when multiple statements are written in a single line.
- While import statement is not shown in the above simple programs, they can both be part of a program in Java as well as Scala. Scala provides an easier and more concise way to import multiple statements as we will see in a later post.
- The major contrast between the two programs is that while the Java program has a class declaration, the Scala program has an object declaration.
Conceptually, a class is a blueprint for objects. An object is a concrete instance of a class.
In Java, we create an object using the new keyword followed by the class name. In Scala, we can directly define an object as shown above. We can also, of course, define classes and create objects using new keyword as in Java. The reason why we have defined an object instead of a class in the equivalent Scala program is described in a short while.
- Before we move on, in both programs, the Java class and the Scala object are public. In Java, the modifier named public needs to be used to mark a class/class member as public. In Scala, by default a class/class member is public. That is, when there is no access modifier, it means that the class/class member has public access.
- Main Methods are defined in very similar yet different ways in Java and Scala programs. An illustration for the main method definitions as they compare with each other in the two languages follows –
def is the keyword that marks the beginning of any function definition in Scala. There is no such equivalent keyword in Java. The main point of difference is the absence of the static keyword in the Scala main method definition.
As Scala is a purely object oriented language, there are no static things in Scala. Nevertheless, in order to achieve a similar behavior, Scala allows defining of something called as singleton objects. Just for now, a singleton object is one which looks like a class definition but with keyword object instead of class. In the above Scala program, Main is a standalone singleton object. To put it simplistically, for a Java programmer, a singleton object in Scala, is an equivalent holder of static methods if it were Java. So, now we know why we have defined an object in the above Scala program instead of a class.
The next important thing that can be noticed is the keyword ‘Unit‘ in the Scala main method definition. Unit is a return type for a function. Unit is used as a result type in a method, if all the method does is produce a side effect and not return any specific value as such. In our case, the side effect is printing out “Hello World”.
- Both the programs use curly braces to mark the beginning and end of method or class.
- While Java implicitly imports members of the package java.lang into every Java source file, Scala implicitly imports members of the packages java.lang and scala as well as members of the singleton object named Predef into every Scala source file.
The println method call in the Scala example above is actually made on Predef.
- In Java, a public class must be in a file of the same name. In contrast, in Scala, a public class need not necessarily be in a file of the same name. Though, it is thoroughly recommended to have it in a file of the same name just to make it easier for the programmers to locate classes.
- Now, lets try and run the above programs at the command prompt.
Java :
$ javac Main.java $ java Main $ Hello World
Scala:
$ scalac Main.scala $ scala Main $ Hello World
As we can see, running the programs in the two languages is pretty similar. The output of a java compiler as well as a scala compiler is a Java class file. This class file can then run on the same JVM to produce the output
$ Hello World