Moving Past Java

Moving Past Java

Now in its 21st Year, Java has had a few rough years. It’s been all but wiped out in the browser area because of security flaws, many Java evangelists have left Oracle, and there’s speculation that Oracle might ditch Java EE as it stopped development on it last year. That’s the same year that saw Oracle lose its case against Google for using Java APIs in Android though Oracle has said that it will appeal.

In the old days, some twenty years ago language comparisons were all about Java vs C++ then in the mid-noughties it became Java vs C#.  C# has won for general computing but Java still has the edge in enterprise computing. However the language has fallen behind C# technically speaking and that’s before other programming languages are mentioned. It’s perhaps not a legacy language yet, particularly thanks to Android but it’s on the way there.

Alternatives to Java

Perhaps it’s time to consider alternatives to Java. I’ve limited this to languages that run on the JVM, (Java Virtual Machine). That gives us quite a choice but I’m going to restrict it further to just two: Kotlin and Scala because of their closeness to Java. If you want to see all of the rest, this JVM Programming languages page on Wikipedia is quite comprehensive.

 

Kotlin

Though developed by a private company JetBrains, Kotlin was open sourced five years ago. Its aim is to be a better language than Java but 100% compatible so you can start with a Java codebase, build it then start taking advantage of Kotlin.

Kotlin has (as does Swift, Go, and Scala ) variables declared before the type, the other way round from C, Java etc.  It’s an object-oriented language with support for higher-order functions as well as lambda expressions and top-level functions. It has taken steps to prevent null pointer exceptions.

 

fun sayHello(maybe : String?, neverNull : Int) {

   val name : String = maybe ?: “stranger”

   println(“Hello $name”)

}
Here the parameter maybe is a nullable string, denoted by the ? suffix on string. The line “val name : String = maybe ?: “stranger”” in the function uses the ?: (called a null coalescing operator) to specify the default value “stranger” for the name string if the variable maybe is null.
Kotlin stops null pointer exceptions by forcing you to add a ? on nullable variables such as person below when accessing name.

 

val person: Person? = null

person?.name = “David”

 

This prevents the app from crashing.

As you’d expect from a language that is 100% compatible with Java, you can use it to develop Android applications with no performance impact and a very small runtime. However it can also generate JavaScript so you can create code that runs in the browser or even server side on node.js.

 

Scala

If you started life as a C developer then moved to C++, you’ll get a sense of deja vu moving from Java to Scala. It’s a contraction of “Scalable Language” and was designed to fix some of the problems with Java. For instance by adding generics, having everything as an object etc.

As with Kotlin, it adopts the Pascal definition ordering for variables which is variable : type.

 

val total: Double = 0.0

 

Also noticeable, and Kotlin has this as well is the lack of semi-colons. This makes the code easier to read and understand.

In Scala every value is an object and every operation is a method call. Here’s Hello World showing this with main being a method of the HelloWorld object.

 

object HelloWorld {

  def main(args: Array[String]): Unit = {

    println(“Hello, world!”)

  }

}

 

Introducing Scala to a Java code base lets you start by removing semi-colons then shift to adding greater immutability perhaps by adopting a more functional style of programming.

You definitely couldn’t do the first line below in Java as int is a primitive data type and not a class as in Scala. The second line below defines a single line function.  Look, no return statement!

 

val numString = “75”.toInt 

def cube(x:Int) = x * x * x 

 

All values in Scala are objects, including numeric and functions. Scala supports both programming styles (functional and object-oriented) . You can also have higher-order functions which take other functions as parameters or return them.

There’s much more to Scala than I’ve touched on. I’ve not mentioned singleton objects, variances, polymorphic methods and many more.  In particular I didn’t mention scala.js which lets you run Scala code in your browser as JavaScript.  Not just JavaScript but ES6! The big advantage, other than terseness is the static typing of Scala.

 

Conclusions

Kotlin was designed to be similar Scala but much faster at compiling though it’s by no means as complicated.

If you are looking for a language to learn and you already know a bit of Java. I’d suggest Kotlin to start with but then move onto Scala.

Jobwise Scala jobs are far more common than Kotlin but that probably reflects the relative ages. Kotlin only reached release 1.0 in February 2016, Scala did that 12 years ago. Scala jobs also tend to be mainly in the finance industry and thus better paid but relatively uncommon. Doing a search in the UK produced 110 job listings in the UK and 1000 in the US but as always, divide these by about 3.5 to get the true number of jobs on offer!

 

 

 

 

 

 

Post a Comment

Your email address will not be published.