Java vs. Groovy, Scala, Kotlin – Language Features Comparison of JVM Languages with code examples

This article compares Java vs. Groovy vs. Scala vs. Kotlin on the basis of several language features using code examples. For basic syntax, compilation & execution comparison refer to Java vs. Groovy, Scala, Kotlin – Basic Syntax Comparison of JVM Languages

Features Summary

This is a quick table of summary of comparison. Scroll down further for in depth examples & actual code comparison.

Feature Java Groovy Scala Kotlin
Non-object Primitives
Type Inference
Since Java 10
Traits / Interface with abstract methods
Since Java 8
Closures
Since Java 8
Duck Typing
Null safety
Higher order functions
Since Java 8
Inline function
Operator overloading

Non-object primitives

Java & Groovy does have primitives which are not really objects. There are separate wrapper object classes available for the each primitive type. On the contrast, Scala & Kotlin does not have primitives at all. All number types are objects. In other words, everything is object in Scala & Kotlin.

Scala:

Kotlin:



Type Inference / Optionally typed

Type inference means automatic detection of data type instead of statically typed data type.

Java:

In java, type of variable must be defined i.e. type inference was not supported. Like in below example String type is defined for variable s. From Java 10 onwards, Java supports type inference using  ‘var’ Example

Groovy:

Scala:

Kotlin:



Traits / Interface with default methods

Traits are interfaces with method implementation/method body.

Java

Before Java 8, this was not supported. From Java 8, interfaces support default methods. Example

Groovy

Scala

Kotlin



Closures

Closures are the instance of a method/function which can be passed to method as argument or returned from a method.

Java:

Before Java 8, this was not supported. From Java 8, in the form of lambda this is supported. Still lambda is dependent on functional interfaces.

Groovy:

Scala:

Kotlin:



Duck typing

Duck typing is way to determine behavior of class on basis of method presence instead of type. So type does not determine at compile time if certain behavior is suitable or not. At runtime, method presence is checked in object & executed or else error. Duck typing is only supported in Groovy. Others i.e. Java, Kotlin, Scala doesn’t support duck typing.

Groovy

Scala

Scala doesn’t allow duck typing & catches it at compile time itself.



Null Safety

Groovy & Kotlin does provide few ways to gracefully prevent NullPointerException as shown in below exception. Java & Scala do not have features to specifically prevent NullPointerException & developers are expected to handle it explicitly.

Groovy:

Null safe access operator ?. which is equivalent to  if(p not null) then p.employer else null
aa

Scala:

No null safety. Standard null checks needed like Java.

Kotlin:

Compile time check for null assignment.



Higher order functions

Higher order functions are methods/functions that take other method/functions as method arguments.

Java:

Since Java 8, java is adopting functional programming. Higher order functions are not directly available, but indirectly can be implemented using functional interfaces like Function. These functional interfaces wrap behavior within an object which can be passed to other methods.

Groovy

 

Scala:

Kotlin:



Inline functions

Inline function instructs compiler to insert complete body code of the function wherever that function is called in code. This is a lightweight alternative to higher order functions which calls for creation of object wherever they are used. With inline function, code is placed at call site at compile time. This is generally used for smaller behaviors.

Kotlin supports inline functions. Couldn’t see any such support in Groovy, Scala & Java.

Kotlin



Operator overloading

Operator overloading is a way to add additional purpose for operators depending on its arguments.

Groovy

Groovy has special methods like plus() which can be overridden to implement operator behavior for given class’ objects. Reference

Scala

In Scala operators are not really operators but they are actually methods unlike Java. So basically operator overloading is nothing but defining method/function with name as operator like +. Reference

Kotlin

Kotlin compiler translates + operator into plus() method & then execution happens like any other method. Reference



See Also:

Java vs. Groovy, Scala, Kotlin – Basic Syntax Comparison of JVM Languages

 

3 Replies to “Java vs. Groovy, Scala, Kotlin – Language Features Comparison of JVM Languages with code examples”

  1. One comment to this very amazing comparison: You claim that Scala does not support any remediation for null pointer dereferences (“Null safety”). This is not strictly true, since the programming language provides the special keyword ‘Option’ which offers safe “Null”s by using a slightly different approach (https://alvinalexander.com/scala/scala-null-values-option-uninitialized-variables). Maybe put some footnote explaining that.

    1. Good point. Scala Option has features kind of similar to Optional in Java. This definitely helps to achieve null safety if incorporated appropriately within code.

      1. In addition, do note that Scala only allows nulls to maintain compatibility with Java. Standalone, Scala never uses nor encourages null values. So yes, Scala does not gracefully handle nulls, but that’s just because its not supposed to. Any operation in Scala that could fail will return an Optional instead of a potential null. And any Java operation can be wrapped inside an Optional to maintain null safety.

Leave a Reply

Your email address will not be published. Required fields are marked *