Kotlin is a new and open-source programming language developed by JetBrains. They are the official developers of the most famous Java IDE, named Intellij IDEA. In this Kotlin tutorial series we will learn Kotlin from the beginning. Kotlin is a statically typed language, which means that all the types of variables used in the code should be known at the compile time. Kotlin runs on JVM, same like Java, Kotlin code is converted into the byte code and then its executes on (Java Virtual Machine) JVM.
In 2017 Google has announced it as an alternative language for Android development after Java. In 2019 Kotlin is announced as a preferred language by Google. And nowadays most of the new native Android Apps are being developed in Kotlin. The good thing is, in a single Android App we can easily use both languages Kotlin & Java without any hazard.
In this series of tutorials, we are going to learn Kotlin from the beginning and will discuss its coding examples related to Android development. But firstly let’s discuss its prerequisites, advantages, and disadvantages.
This tutorial series is specifically designed for beginners who may not have any knowledge of Kotlin or Android development knowledge before. But it is good if you have basic programming knowledge, otherwise, you still can continue as we tried our best to make this tutorials series as simple as possible.
Why Kotlin over Java? Advantages & Disadvantages
As mentioned before Kotlin is a brand new language having many more features that we don’t have in Java, not even in Java 11. The following are the advantages of Kotlin over Java. A few disadvantages are also mentioned below.
1. Short Program & Easy Coding
Thanks to Kotlin that we don’t need to add semicolons anymore at the end of every statement. Also by writing the same program in Kotlin usually we can do it in smaller code as compared to Java. Kotlin is more readable. No more Static declarations, so code remains organized and easily understandable.
2. No More Null Pointer Exceptions
Unlike Java, we cannot put null in a normal reference, as the compiler does not allow us to do. If we wish to set a null reference in Kotlin we need to declare it as a nullable reference. So generally, the chances of Null pointer exceptions are reduced in Kotlin, until we force a nullable reference to unwrap using !! keyword. See the following example,
String str = null; // Legal in Java
val str: String = null // Illegal Kotlin, with error, Null can not be a value of a non-null type String val str: String? = null // Legal in Kotlin
We will learn it in detail in next sections.
3. Interoperability & Java Compatibility
We can use Java & Kotlin in the same project and can exchange information easily between Java & Kotlin classes. So it means we can easily use existing Java libraries, SDKs & frameworks easily in Kotlin Projects. In Android Studio if we paste a Java code into Kotlin File it automatically prompts for the syntax conversion to Kotlin because of a Pre-added Kotlin Plugin.
4. No Static Declaration
In Kotlin, we no more have static declarations which means that the code remains understandable and less complex. On the one side, it can be an advantage, but on the other side, it can be a disadvantage as well. Although we can have class Companion Objects to tie a function or property to class instead of to its object.
5. Data Classes in Kotlin
Kotlin introduced the concept of data classes. In Java, we have to write a lot of boilerplate code in data classes but now in Kotlin, we can use data classes to achieve the same functionality by writing less code.
6. Other Extra Features In Kotlin
We have many extra features in Kotlin which we don’t have in Java and other conventional languages, Like late initializations, Separate Mutable & Immutable collections, Extensions, data classes, companion objects, and nullable & non-nullable references. We will learn about all of these in later tutorials.
If you have any questions or suggestions related to this Kotlin tutorial, feel free to ask in the comments section below.