This is the part 1 of series Java to Kotlin - Learning Simplified. This part is to answer one simple question, Why Kotlin? I hope you learn something new from every part. If you are already convinced why do you need Kotlin and want to jump into code, check Part 2

Java was officially launched in 1995 and it has matured a lot since then. No doubt, Java has its fair share in programming language world and it has always been among most popular languages around. Why would someone come up with a new language interoperable with java, close to Java and claim it is better!

Jetbrains, behind a very famous IDE IntelliJ is the force behind Kotlin. They used to write a lot of code in Java and felt need of change.

A couple of pain points they felt:

πŸ‘‰ Java is verbose which is good to some extent but many times developers spend time writing boilerplate code OR a code which could easily be taken care by Compiler

πŸ‘‰ Java has notion of null which is a pain. They thought can a compiler be smart to figure out nulls and help developers rather than punishing them at runtime

πŸ‘‰ Can we create a smarter language on top of Java principles which is high level in true sense. It means to learn from the mistakes of existing Java codebase, ask developers what they want to improve and create powerful utilities+features in the language

πŸ‘‰ Can a language be focused on the problem solving aspect rather than trivial things like where to put curly braces

πŸ‘‰ Last but not the least, can we develop a language which is 100% interoperable with Java. It means both the languages should be able to call each other without a slight friction

πŸ‘‰ and few more…

..And that’s how Kotlin was born. A language conceptulized by an IDE company to solve their own pain points. Later, developers realized they were also having these pains but never realized because no one pointed and there was not a better solution. Google also supports Kotlin officially in Android.

πŸ”₯ Fun Fact: Kotlin was named after an Island in Russia

Initially you might not like Kotlin syntax and ideology. The reason behind is not that Kotlin is bad or good, but it is that your mind does not like change. It gets comfortable and considers change as friction. Read all the parts and check all the examples before you conclude. Keep an open mind is the secret of growth.

Glimpse of Kotlin Flavor
Kotlin has many smart offerings, one of my favourite is SmartCast by Smart Compiler.

In Java if you need to use the subclass method, you need to check instance of at runtime and explicitly cast. You use following code:

if(obj instanceof SubClass) {
	((SubClass)obj).callSubClassMethod(); // explicitcast πŸ‘Ž
}


However, In Kotlin there is smartcast, The compiler is smart enough to know whether an explicit cast is requried or not. e.g

if(obj is SubClass) {
	obj.callSubClassMethod() // smartcast πŸ‘
}

πŸ“ Note: The Kotlin compiler smart casted the obj to SubClass because it knows the code will be executed only if instanceof check is passed. We use is keyword to check instanceof in Kotlin. It is a very subtle example to show power of Kotlin.

3 core Principles

We will discuss these 3 principles in our series: Concise, Smart and Powerful. If you were Kotlin developers, how will you make anything in java concise OR smart OR powerful. For every question, you have to come up with an answer and match your answer with Kotlin way.

Think how will you apply these 3 priciples in our Casting example. I personally believe smart cast is better than explicit cast. I used to think I am trading readibility with concise code but it varies from use case to use case. My suggestion in casting example is, if you are using the subclass method immediately after instanceof check, you do not need explicit cast, if you are using somewhere down in the if block, you are free to cast explicitly. Kotlin does not stop you, it empowers you.

Explicit Cast Example

	if(obj is SubClass){
        var subIbj:SubClass = obj
	}

        OR

	var subObj:SubClass = obj as SubClass

Summary

Kotlin is developed with a mindset to give wings to Java programmers. If Java is compiled, Kotlin is smartly compiled. It is modern and 3 core principles we will be focusing are Concise, Smart and Powerful. Small things like smartcast make difference in large projects. Lesser you type, lesser code is to be maintained. Spend time and energy in solving your business problem.

It is possible that you do not face any issues with Java like Jetbrains did. It is also possible that you are not going to like Kotlin (though the likelihood is less). Needless to say, it is a language designed from ground up in 21st century and definitely a good tool to have in your toolbox. Plus the possibility of mixing it with Java code enables you to try as per your own convenience. My suggestion is to give it a shot and make an informed decision.

Let’s jump into real code and see the difference between Java and Kotlin. Here is part 2.

Subscribe to newsletter and keep learning good stuff

Email

πŸ‘‰ If you like article, click on β™₯️ recommend and share on social πŸ‘₯ media.
πŸ‘‰ Feel free to comment πŸ’¬
πŸ‘‰ Follow me πŸ‘€ for the more interesting articles. Twitter Medium
πŸ‘‰ Subscribe to newsletter and keep learning