Scala vs Java examples

Tags: , ,

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:

A hello world example for Java:

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

 

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:

 

Use Scala function as a argument:

Use Scala function as the returned value from another function:

 

Scala vs Java: Classes

Similar to Java, Scala also has Classes. One important difference is that classes in Scala can have parameters. For instance, 

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:

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:

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.

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.