Moving from Java to Kotlin with Scala stopover

Piotr Chorościn
Jul 31 · 4 min read

Java Virtual Machine is home to wide range of programming languages. Recently one of them, Kotlin, is pulling ahead. Can a Java programmer can take advantage of this trend?

All we need is Java!

Java was one of the very first high-level programming languages that I came across as a computer science student. At that time I was astonished by the convenience of garbage collector — the feeling of being released from burdensome memory management process that I knew from C and C++98 was awsome.

That said, I have to admit that writing getters and setters for private fileds that made them actually behave like public ones — just for sake of encapsulation rule — seemed nonsense for me. Even though your IDE can generate it for you — it still produces plenty of boilerplate code.

Another pain in the neck was coexistence of primitive types and their objective counterparts. Want to make ArrayList<int>? Forget, you need ArrayList<Integer>.Whilst autoboxing mechanism makes it easier to use those types interchangeably, it still may be confusing. Oh and all these hashCode, compareTo stuff. And constructors. And …

Wait, but it is inevitable. At least that was what I tought.

Scala => stranger in my house

Being content, but unexperienced Java programmer I suffered this ucomfortable feeling of confusion when I encountered new subject at the second year of Computer Science. It was called Programming Paradigms.

Okay, we have object-oriented programming and it works fine, what else do I need? In the very first lecutre of PP it came and turned my object-oriented word upside down — functional programming.

First, the idea of immutability — it contradicted my whole concept of program as list of instructions that actually do change its state.

Second, functions as first-class citizens. Passing a function to a variable — let’s say x and then invoking it like x(5), where 5 is argument passed to x function?

My brain was on fire. And than comes the hard part: lack of loops — everthing is done through recursion! At least that was the case when we were made to program in OCaml (part of Meta Language family) in a functional style.

As an alternative to Objective Caml (whose objective features were forbidden at this degree of initiation) Scala programming language was introduced.
Its certain degree of resemblance to Java’s syntax made it more accessible for me at that time. Java’s libraries, classes and methods can be used in Scala. Scala is compiled down to the same bytecode as Java and runs on the same virtual machine.

Nonetheless, differences are significant. Then I learned how functional and objective programming styles can be combined. Just use var instead of val and here you have your old, mutable friend again. However, now it seems far more distant, since you are familiarized with concept of immutability.

And the fun part. Because as my lecturer used to say:

In functional programming everything is (a) fun.

No more primitive types — simple Int object instead of competing int and Integer. Whilst those features seem to be a blessing, some Scala concepts still pose a challenge for me — e.g. Traits. So after passing Programming Paradigms exam I decided to come back to Java’s mainstream.

Kotlin : Here we go again

In the semester following the one in which I was put trough the hoops of functional programming, I started learning Android platform development.
Lectures where held in Java-style, but during labotaries free rein was given to us. So I took up a challenge to learn new language,now first-class citizen in Android app development: Kotlin.

Little did I know how much I would owe to that decision.

What stroke me first, was the strong resemblance to Scala. Long time no see, val keyword! Oh, and when expression. And sealed classes. And C#-like properities. I felt home!

But wait, the best is yet to come. Let one who didn’t get into infamous NullPointerException cast the first stone. But in Kotlin’s world it is highly unlikely to do so. How it’s possible?

Kotlin introduces idea of non-nullability. To put it in layman’s terms: is null a correct instance of String class? Or any other class? Well, it depends…
Whilst in Java, assigning null to a variable of type String sounds fair enough, that’s not the case in Kotlin. To be able to do so, you have to declare varable’s type as String?. Yes, the question mark is not a typo. It basically means that variable is of type “String or null” or so-called: nullable String.

And they are not interchangeable, what prevents programmers from NPE. Checking nullability is required before assignment. It can be done gracefully with use of so called Elvis operator ?: I guess the name comes from resemblance to Elvis’ hairstyle. However, it’s still possible to get NPE in Kotlin if you explicitly ask for it, using not-null assertion operator: !!.

Having said that, I have to admit that the set of features mentioned above is only the tip of the iceberg. Other cool stuff like primary constructors, initialization blocks, extension functions, default class finality and coroutines are worth further lecture. If you are into mobile programming, please take the promise of Android development process’ acceleration for encouragement.

Conclusion

Neither is this article an exhaustive introduction to Kotlin nor tutorial on Scala. It is simply invitation for Java programmers to take its modern cousines into account when starting a new project. Although since Java 8 many features can be achieved through annotations, Kotlin and Scala handle them on a daily basis. Oh and please, do not limit usage of Kotlin to Android development. Google was simply the first that officially acknowledged Kotlin as the heir to JVM’s throne. First, but not last.

Piotr Chorościn

Written by

Computer Science student. Bechelor of engineering in Robotics and Control engineering.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade