How Kotlin can cover for the shortcomings of Java? — A Developer’s Outlook

Systango
6 min readAug 22, 2019

--

Android App Development with Kotlin — Systango Blog

According to the survey done by EDC in 2018, there are around 23 million Software Developers around the world. This figure is estimated to increase by six-folds in upcoming years. Needless to say, “software developers are the architects of the new world.”

Fascinatingly, as you read this article, there are millions of developers around the world trying to create their next innovative software — and close to 9 million of them are using JAVA, as per a study conducted by Oracle.

An important question was posed to all the JAVA developers across the globe:

“What is the single most painful problem they face while developing a software application?”

“The Null Pointer Exception”.

While writing any software program, a JAVA developer ends up with lots of null checks. If they forget the null check, then the application will crash. Though Java 8 was introduced as a solution to overcome the null check but instead of making life easy, they put more burden on programmers i.e. first wrap the actual value in a wrapper then checks of is present and so on.

Is there any other programming language which can be easier and readable to handle these exceptions?

The answer is Kotlin.

Kotlin is a statically typed JVM language developed by Jetbrains in 2011. It is interoperable with Java, and the JVM version of its standard library depends on the Java Class Library. It mainly targets the JVM, but also get transpiled to JavaScript or native code. Very Soon, Kotlin got the attention of some big brands like Amazon, Netflix, and Pinterest. It has been receiving popularity in the world of programming.

Merits of Kotlin

  • Concise — You can “do more with less code”. As a result, your code will be easy to use and easy to change.
  • Enforces best practices — While declaring a field, you have to specify val or var before it to decide whether it is mutable or immutable. By default every class in Kotlin is final. To allow inheritance, you have to specify a class as open explicitly.
  • Has both OO and FP style — Kotlin is not a fully object-oriented language. You can use it as object-oriented or you can write your code without using classes also.
  • Interoperable with Java — Java code can be used in Kotlin and Kotlin code can be used in Java. Also, it’s easy to convert Java code into Kotlin using Android Studio or various online converters. To use Kotlin in existing Java project, you don’t need to rewrite everything from scratch. You can start writing new classes in Kotlin and if needed, can convert existing Java code to Kotlin gradually.
  • User-friendly Keywords — Kotlin replaced switch statement with when. Similarly, introduced “is” instead of instanceof. Kotlin comes with real-life keywords which makes the program more expressive.

Cool features of Kotlin

  • Easy Null Pointer Handling — As discussed initially, Kotlin provides easy Null Pointer Exception handling without using too many null checks. If there is any chance of null pointer exceptions, Kotlin compiler will give you an error at compile time. It provides safe-calls, non-null assert and Elvis operator to deal with null values.
  • Smart Cast — Let’s look at the example below:

Typecasting in Java-

Code — Android App Development with Kotlin — Systango Blog

Typecasting in Kotlin-

Code — Android App Development with Kotlin — Systango Blog

If checking is already done of a person is the instance of Employee, you don’t need to cast it again and again. This is called smart cast.

  • Data Classes — To create POJO or Model or Data Holder classes, you do not need to write lots of boilerplate code i.e. getters/setters, toString(), equals(). Though Java has libraries like Lombok for this purpose, Kotlin comes with first-class support of data classes.
  • Easy Singleton — We will see the singleton with the undermentioned example:

Singleton class in Java-

Code — Android App Development with Kotlin — Systango Blog

Same singleton class in Kotlin-

Code — Android App Development with Kotlin — Systango Blog

Make a class object and you don’t need to write boilerplate code to create a singleton class.

Named Arguments — If you look at below method call, you will not know what each argument is doing-

In Kotlin, a developer can provide names to the arguments as follows-

Code — Android App Development with Kotlin — Systango Blog

Now, this method call is more descriptive.

Domain-Specific Language(DSL)– You must have seen below code in gradle based projects-

Code — Android App Development with Kotlin — Systango Blog

It’s called DSL where you can write code in English like syntax. You can make the same method calls in Kotlin. There are plenty of in-built and third-party libraries which provides DSL i.e. String Spec for unit test cases.

Internal Access Specifier- Java has several access specifier i.e. public, private, protected and default. Suppose a developer is developing an SDK and don’t want SDK users to access that class, what will you do? A developer cannot make it private or default because it is used in another package of your SDK code. If it is made for public access then it can be used from SDK client. Kotlin introduced new access specifier internal. An internal class/field can be accessed within a module only.

Apart from the ones mentioned above, Kotlin has a lot more features i.e. more descriptive and easier functional programming, nested functions, type alias, inline functions, extension functions, etc. Find more features and syntax on the official website of Kotlin.

Where to Use?

Kotlin is a language which can be used to create Android applications, developers can create native libraries which can be used in multiple platforms. It allows writing back-end code and benefits to create desktop applications with it. Let’s see the popularity of Kotlin and famous companies which are using it-

You can use Kotlin with all popular IDEs i.e. IntelliJ IDEA, Android Studio, Eclipse, Visual Studio Code, etc. Kotlin is Getting Prevalent and has quickly skyrocketed in popularity-

Hopefully, the above information successfully introduced you to Kotlin.

Our team at Systango has created a bunch of mobile applications using Kotlin with less code and more functionality. Let’s connect and build your upcoming Android app using Kotlin.

We breathe, eat, sleep…Technology!

This article was originally published on Systango.

--

--

Systango
Systango

Written by Systango

We are an Award-Winning Digital Engineering & Software Development Studio. We deliver digital experiences and reinvent your applications with modern tech

No responses yet