Sponsored Links

Ad by Google
Scala is getting much and more popular, and become one of the favourite alternative of Java that's the reason why Scala grown exponentially. Scala syntax is very much similar to Java, and the best is that Scala runs on JVM(Java Virtual Machine), that means Scala is fully compatible with Java syntax, one can easily mix both code Scala and Java on same class.We have already seen an overview of Scala in my previous article.
After compiling the Scala code, it will generate the .class file similar to Java, which makes it write once run anywhere compatible, even you will get all the objects/method of java.lang package by default into Scala code. Although Scala is very much similar to Java, but still has some major differences, which caused the invention of Scala, and those are listed below.
Differences between Scala and Java
1. Scala is pure object oriented with fully support of functional programming

The first and major difference between Scala and Java is that, Scala is pure object oriented, that means everything in Scala is an Object including numbers or functions. Whereas Java is not pure Object oriented programming language because of primitive types like int, bool, double etc. Where you can not call functions on these, but in Scala nothing like primitive everything is object, so you can easily call any method on Int, Double etc.
And also Scala is fully supported of functional programming, but Java is not.

2. Less amount of code in Scala as compared to Java

The simplicity of Java is much better as compared to Scala, but while coding Scala will reduce the number of lines to write by fully utilizing the type inference, as compared to Java.
Quick example, to add fruits names into a list.
In Java:
List<String> list = new ArrayList<String>();
In Scala:
val list = List("Apple", "Mango", "Banana","Papaya")
Just a single line , really awesome isn't it?

3. Scala support operator overloading, Java does not

Scala supports operator overloading whereas Java doesn’t.

4. Scala needs learning curve, as compared to Java.

Scala is not that much simple as Java, one of the big reason behind the popularity of Java is simplicity. Java is more simple, but Scala is not, Scala needs thorough learning curve, because Scala is more complex as compared to Java, Scala has too many nested support. Although syntax of Scala is very much similar to Java, but the representation of Scala code is not that much simple.
Note: of course this point is vary depending upon the user to user :) but it's from my personal experience.

5. Scala has built in lazy evaluation, but Java does not

Scala supports lazy evaluation with the help of lazy keyword, you can evaluate values lazily. This will very much helpful while dealing with values that are not required during execution and also the computation cost is very high, for that it's much much better to get the computation on demand, for more on lazy evaluation in Scala.

Below is the syntax of lazy evaluation:

Without lazy evaluation:
scala> val x = 10
x: Int = 10

With lazy evaluation:
scala> lazy val y = 15
y: Int = <lazy>

here (lazy val) is not executed,unless you accessed it for the first time. Here is a tutorial to set-up Scala in your lappy to start with.

That's it, if you know any other differences, please do let me know in comments, so that I can add into the list and it'll really help someone too.
Sponsored Links


Post a Comment