How Kotlin can cover for the shortcomings of Java? — A Developer’s Outlook
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.
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-
Typecasting in Kotlin-
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-
Same singleton class in Kotlin-
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-
Now, this method call is more descriptive.
Domain-Specific Language(DSL)– You must have seen below code in gradle based projects-
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-
- It is used by Google, Amazon Web Services, Pinterest, Coursera, Netflix, Uber, Square, Trello, Basecamp, Atlassian, and many other top leading companies.
- Corda, a distributed ledger developed by a consortium of well-known banks (such as Goldman Sachs, J.P. Morgan, HSBC, etc), has over 90% Kotlin in its codebase.
- Prezi — A presentation software company using it in the backend which has over 100 million users.
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-
- Google declares Kotlin as the primary language to develop Android apps in Google IO 2017.
- It was voted one of the five most loved languages according to Stack Overflow.
- It is the fastest-growing programming language according to GitHub.
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.