Scala vs Java examples
This tutorial gives a quick introduction to the Scala language by comparing Scala with Java using examples. It is for people who have learned Java and want to learn Scala.
Scala vs Java: The Hello Word Example
A hello word example for Scala:
|
1 2 3 4 5 |
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } |
A hello world example for Java:
|
1 2 3 4 5 6 7 8 |
public class HelloWorld { public static void main(String[] args) { // Prints "Hello, World" to the terminal window. System.out.println("Hello, World"); } } |
object Keyword in Scala
In scala, the object keyword is used to create a singleton object. The declaration above declares both a class called HelloWorld and an instance of that class, also called HelloWorld.
One difference between Scala and Java is that, the main method is not declared as static. This is because, in Scala, static members (methods or fields) do not exist. In Scala, we declare these members in singleton objects instead. However, you need to write more code to implement singleton pattern.
Scala vs Java: Compile programs
We use scalac to compile a scala program, but we use javac to compile a Java program.
Suppose we save the above scala program in a file called HelloWorld.scala, we can compile it using the following command:
> scalac HelloWorld.scala
This will generate a HelloWorld.class in the directory, which can be directly executed using the scalacommand,
Scala vs Java: Run programs
Once compiled, a Scala program can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output
|
1 2 |
scala -classpath . HelloWorld Hello, world! |
Scala program can use Java Object directly
Both Scala and Java are JVM based programming language. One advantage of Scala is that you can use Java libraries in Scala directly.
All classes from the java.lang package are imported by default, while others need to be imported explicitly.
When you develop a scala program, you can easily use any Java libraries such Google Guava, Apache Opened sourced Java libraries like Lucene.
In Scala, everything is an Object
Numbers are Objects in Scala
Java is an object oriented programming language, but in java, there are non object such as the primitive types (i.e., boolean and int). In Scala, however, everything is Object.
So numbers are object, functions are object
When you execute an arithmetic expression 1 + 2, it actually means you have two objects 1 and 2. the object 1 has a method named +, you call that method on the object 1 with the argument 2. You can run it as follows:
(1) .+ (2)
So + is actually a method name in Scala. Unlike Java, you can’t use symbols such as +, _, * as a method name, In scala, they are valid identifiers.
Scala Function are objects
Different from Java, Scala supports functional programming by making functions as objects. So you can use functions as arguments of a method. Store a function into a variable, and event return a function from other functions.
Here are some examples:
Store a function into a variable:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Test { def add(a:Int, b:Int): Int = { return a + b; } } object Main { def main(args: Array[String]) { val test = new Test() val myAdd: (Int, Int) => Int = test.add print(myAdd(1,3)); } } |
Use Scala function as a argument:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Test2 { def myPrintln(a:Int, b:Int, f: (Int, Int) => Int): Unit = { println(f(a, b)) } } object Main { def main(args: Array[String]) { val test = new Test() new Test2().myPrintln(2, 3, test.add); } } |
Use Scala function as the returned value from another function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Test3 { def saySth(prefix: String) = (s: String) => { prefix + " " + s } } object Main { def main(args: Array[String]) { val sayHello = new Test3().saySth("Hello") println(sayHello(" Learn4master")) } } |
Scala vs Java: Classes
Similar to Java, Scala also has Classes. One important difference is that classes in Scala can have parameters. For instance,
|
1 2 3 4 |
class Complex(real: Double, imaginary: Double) { def re() = real def im() = imaginary } |
This Complex class takes two arguments, which are the real and imaginary part of the complex. These arguments must be passed when creating an instance of class Complex, as follows: new Complex(1.5, 2.3). The class contains two methods, called re and im, which like getters in Java.
It should be noted that the return type of these two methods is not given explicitly. It will be inferred as Double automatically by the compiler.
Simplifying methods call when there is no argument
To use the re and im methods, we have to put an empty pair of parenthesis after their name. For instance:
|
1 2 3 4 5 6 |
object ComplexNumbers { def main(args: Array[String]) { val c = new Complex(1.2, 3.4) println("imaginary part: " + c.im()) } } |
In Scala, we can simply the call by defining them as methods without arguments. Such methods differ from methods with zero arguments in that they don’t have parenthesis after their name, neither in their definition nor in their use. We can rewrite the Complex class:
|
1 2 3 4 |
class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary } |
Scala vs Java: Inheritance and overriding
All classes in Scala inherit from a super-class. When no super-class is specified, as in theComplex example of previous section, scala.AnyRef is implicitly used.
In Scala, it is mandatory to explicitly specify that a method overrides another one using the override modifier, in order to avoid accidental overriding.
For instance, the Complex class can override the toString method from Object.
|
1 2 3 4 5 6 |
class Complex(real: Double, imaginary: Double) { def re = real def im = imaginary override def toString() = "" + re + (if (im < 0) "" else "+") + im + "i" } |
Summary
In this post, I use examples to compare Scala and Java. You can see they share many similarities, both Scala and Java are JVM based languages. So you can easily call Java methods in a Scala program.
Different from Java, Scala is a pure Object oriented Language: Everything in Scala are objects. Scala also supports function programming. Functions in Scala are first class objects, they can be stored into a variable, used as function argument and the returned value of a function.
In the next post, I will discuss more features of Scala.











