Java is too old, What should you learn in 2018?
Tags: computerscience, kotlin, java, programminglanguages
Published: 2018-10-10 13:51:23

---
title: Java is too old, What should you learn in 2018?
published: true
description: Java has become legacy. It can't evolve in to a modern language. But it has given us a wonderful JVM ecosystem and lead to creation of many good languages Groovy, Scala, Clojure, Kotlin. Kotlin has brought powerful features from many other JVM languages.
tags: computer-science,kotlin,java,programming-languages
canonical_url: https://hackernoon.com/java-is-too-old-what-should-you-learn-in-2018-12cd0151b2d1
cover_image: https://thepracticaldev.s3.amazonaws.com/i/afwyf93jrllr1u037b8m.jpeg
---


# Java is too old, What should you learn in 2018?

Java has become legacy. It can’t evolve in to a modern language while keeping its backward compatibility. But it has given us a wonderful JVM ecosystem and lead to creation of many good languages Groovy, Scala, Clojure, Kotlin.

## Welcome the newest baby Kotlin

Kotlin was born in 2011, But it gained popularity last year after google announced it as official language for Android. Kotlin has brought powerful features from many other JVM languages. Let take a quick glance at benefits of Kotlin for a Java developer.

### Never worry about NPEs

Every Java programmer hates Null Pointer Exceptions thrown at Runtime. Kotlin provides first class support to avoid null references at compile time. All objects are non-nullable by default and you have to use ? operator to define nullable types. Compiler will force you to use safe call operator ?. to access nullable objects. You can also use elvis operator ?: to assign default values.

{% gist https://gist.github.com/erajasekar/e1ffea75ff936c5bf0b26dd4115ea0c0 %}

### No need to provide explicit type declaration.

Kotlin automatically infers types, so you don’t need to declare it explicitly. You can simply define variables using val for final variables and var for non-final variables. Note type can be inferred only if both declaration and assignment is done in single statement.

{% gist https://gist.github.com/erajasekar/12159d1a10d0e2688c2665a9b3b5ae09 %}

### Avoid convoluted String formatting.

In Kotlin you can use String templates for easier formatting of Strings. $is used to reference a variable and you can use ${} for complex expressions

{% gist https://gist.github.com/erajasekar/17aaaac26909b569f9aeba4504dba1cd %}

### No boiler plate code required to create simple POJOs.

Kotlin provides data classes for objects used to simply hold values. It automatically generates equals , hashCode , toString , copy , getters and setters ( for properties defined as var) methods for data classes. You can also do object deconstruction of data class to extract properties to variables.

{% gist https://gist.github.com/erajasekar/16fbf54a077d31dbd23ce1943d602b00 %}

### You can avoid Builder classes and redundant method overloads.

Kotlin supports named method parameters so you don’t need to create builders in most cases. Also, Kotlin supports default method parameters so you don’t need to create redundant overloaded methods to pass default values.

{% gist https://gist.github.com/erajasekar/4067aceb771c3a75f24c6f2d1db103a5 %}

### You no longer need guava library to statically initialize collections.

Kotlin provides concise way to initialize collections inline using listOf , mapOf , setOf methods. Maps also support intuitive syntax key to value for initialization. It also provides deconstruction of Map key, values for easy iteration.

{% gist https://gist.github.com/erajasekar/58cd6e3ccd8ca6e414448ce6def9487a %}

### You don’t need a complicated way to create Singletons.

Kotlin supports object declaration to create Singletons in single line.

{% gist https://gist.github.com/erajasekar/666515a94f4024d55d063695aa9ea4c0 %}

### You don’t need unnecessary local variables

In Kotlin, constructs like try and when are expressions that returns value. For e.g You can assign result of try to a variable instead of creating a local variable. Similarly for when can be used as expression. when is equivalent to switch in Java, but it is much more powerful.

{% gist https://gist.github.com/erajasekar/c2f2c278f1f60d7200bc385e48c65107 %}

### You will avoid Class Cast exceptions.

Kotlin provides is operator (equivalent to instanceOf in Java) to check if object is specific type . Using is operator will automatically do casting for you. This will prevent Class Cast exception when you cast to wrong type.

{% gist https://gist.github.com/erajasekar/e7598587352283709cd2d4b7e1684206 %}

### You don’t need to repeat the variable name to call sequence methods in same object.

Kotlin provides with construct to easily call sequence of methods on same object without having to repeat the variable name. We generally use builder pattern and method chaining to that in Java. Kotlin makes it easy to do similar thing even for non-builder classes.

Kotlin also provides apply extension function to achieve the same thing.

{% gist https://gist.github.com/erajasekar/0814ec967ee79413dfa1126f4ee3bc14 %}

### You don’t need boilerplate code to use delegation or decorator pattern.

To favor composition over inheritance, we often use delegation or decorator pattern, but we had to duplicate every method of delegated class in wrapper class. Kotlin provides first class support to simplify delegation using by operator. It will automatically implement necessary methods to call methods of delegated class. Of course, you can override specific methods when need to.

{% gist https://gist.github.com/erajasekar/10c94d157bd3c47086fead3201526787 %}

### You don’t need to a Class to create static functions.

Kotlin supports functions outside of classes, so you don’t need to create a class just for static utility functions.

{% gist https://gist.github.com/erajasekar/32899658473ebe40cba08fb76a6cfbf7 %}

### You don’t need a hack to modify non-final variables in lambdas.

Java supports lambda by automatically substituting it with anonymous inner classes, but you can’t modify non-final variable inside lambdas. But in Kotlin you can also modify non-final variables within lambda.

{% gist https://gist.github.com/erajasekar/d48915dc06ef13de54c84e01b6e0747e %}

### Lazy loading doesn’t require hard work.

Kotlin provides very simple way to lazily initialize a property using lazy keyword.

{% gist https://gist.github.com/erajasekar/b441eb6a47a4eec8efdd335ff25b81d5 %}

### You don’t have to totally switch to a new language.

Kotlin interoperates with Java seamlessly, so you can easily integrate with legacy Java code. You can continue to leverage third party Java libraries and frameworks. Unlike Scala, Kotlin doesn’t have it’s own collection library, but extends JDK collections. So you don’t need to write glue code to convert between Java and Kotlin collection types.

{% gist https://gist.github.com/erajasekar/a89ca2b4326e205458df85781a558be4 %}

### You can write concise and more readable code.

Kotlin uses method name convention to overload many operators for readability. For e.g method plus is used to overload + operator, minus for - operator, times for * operator, div for / operator and so on. It supports overloading many more operators like %, += , +- , ++,--

{% gist https://gist.github.com/erajasekar/07f3f0d74aef7bd2f303c527b036328a %}

Kotlin provides concise way to define ranges using .. operator. It also provides until keyword for excluding boundaries and step operator for skipping items. It has in operator to check for something in range. We can also overload .. operator and in operator by implementing rangeTo and contains methods.

{% gist https://gist.github.com/erajasekar/4587739da6525731f5005c5d93eb0d48 %}

### You can easily extend existing JDK classes.

Kotlin support easy way to add extension functions to existing classes. This is a very power feature that helps us to easily expand core language API.

{% gist https://gist.github.com/erajasekar/88ffb5b0ef7bb41be71ffdbbb1e663c7 %}

## Why should you use Kotlin?

### It boosts your productivity by many fold

* It’s statically typed language, so you will catch lot of bugs at compile time.

* It has great tooling support. No doubt it’s created by an IDE company

* It is created by JetBrains a IDE company which cares for developer productivity.

* Avoid many of day to day frustrations of a Java developer.

### You get benefits of many best practices (Principles from [Effective Java Book](https://amzn.to/2qi8VRv)) by default.

* All classes are final.

* Collections are immutable.

* Override mandatory keyword instead of optional annotation

* No Checked exceptions

* No Raw types

* Cleaner support for generics.

### Non-JVM support.

* Kotlin also compiles to Javascript for front end development.

* They are also working on adding native runtime to make it run without JVM.

## Should you switch to Kotlin?

Kotlin is awesome but it’s not yet perfect. It will take some time to evolve in to a great language. JetBrains and Google are actively behind Kotlin so you can sure be that it will only get better.

If you are an Android developer, you should start using Kotlin immediately.

If you are a Java developer, you might have to consider other factors like team members, company adoption etc. But even if you can’t use it immediately, you should definitely learn this modern piece of beauty.

Finally, As per [thoughtworks technology radar](https://www.thoughtworks.com/radar/languages-and-frameworks), companies can adopt using kotlin in projects when appropriate.

## References

* [Should I learn Kotlin or stick to Java?](https://medium.com/mindorks/should-i-learn-kotlin-or-stick-to-java-3a73c3580ac3)

* In GoogleIO 2017 Google announced Kotlin as an official language for Android development. Some famous developer…*medium.com](https://medium.com/mindorks/should-i-learn-kotlin-or-stick-to-java-3a73c3580ac3)

* [Kotlin in Action Book](https://amzn.to/2qiL8Ro) By [Dmitry Jemerov](https://www.amazon.com/s/ref=dp_byline_sr_book_1?ie=UTF8&text=Dmitry+Jemerov&search-alias=books&field-author=Dmitry+Jemerov&sort=relevancerank) (Author),‎ [Svetlana Isakova](https://www.amazon.com/s/ref=dp_byline_sr_book_2?ie=UTF8&text=Svetlana+Isakova&search-alias=books&field-author=Svetlana+Isakova&sort=relevancerank) (Author)