paint-brush
Java is too old, What should you learn in 2018?by@e.rajasekar
11,782 reads
11,782 reads

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

by Rajasekar ElangoApril 9th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Java is too old, What should you learn in 2018?
Rajasekar Elango HackerNoon profile picture

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.

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.

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

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.

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.

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.

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

Kotlin supports object declaration to create Singletons in single line.

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.

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.

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.

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.

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.

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.

Lazy loading doesn’t require hard work.

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

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.

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 %, += , +- , ++,--

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.

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.

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) 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 , companies can kotlin try in projects that can handle some risk.

Update on Oct 10, 2018:

Kotlin has moved up from Trial to Adopt category in thoughtworks technology radar, so companies can adopt using kotlin in projects when appropriate.

References


Should I learn Kotlin or stick to Java?_In GoogleIO 2017 Google announced Kotlin as an official language for Android development. Some famous developer…_medium.com

Kotlin in Action Book By Dmitry Jemerov (Author),‎ Svetlana Isakova (Author)

If you enjoyed this post, Please clap so that it will reach more audience. I am planning to write deep dive articles on some of the Kotlin’s features. If you like to read more about Kotlin, follow me so that you will notified on new stories.