In Google I/O 2017, Kotlin programming language was announced as an official programming language for android application development. With this announcement, Demand for kotlin programming language has increased significantly. So, here, we are presenting you tutorials on Kotlin programming language.
This is my first tutorial on Kotlin programming language. In this post, i will be covering introduction of this programming language.
Kotlin is statically typed programming language for modern multi-platform applications. It is developed by JetBrains. You can develop application for JVM, android and web browser using this language. It’s syntax is more expressive and concise than that of Java. However, Kotlin and Java is interoperable i.e. you can use both language in same application.
Many of you might be thinking why are we using kotlin? Please go through the features of Kotlin programming language to get your answer.
Major features of Kotlin Programming language –
a. Concise
b. Safe
c. Interoperable
d. Tool-friendly
Now, we will go through each feature one by one
1. Concise
As you know very well, Bugs in your code is directly proportional to the total number of lines of code it has. Means, more the number of lines of code, more chances of having bugs in the code. Kotlin language needs just a few lines of code to do the job. It drastically reduces the amount of boilerplate code. For example,
We are just comparing between java and kotlin language. Our job is to create a simple class that contains getters and setters as well as overriding methods equals(), hashCode() and toString(). Let’s see how we can achieve it using both languages
Java
public Customer(String name) { |
public Customer(String name, String email) { |
public Customer(String name, String email, String company) { |
public String getName() { |
public void setName(String name) { |
public String getEmail() { |
public void setEmail(String email) { |
public String getCompany() { |
public void setCompany(String company) { |
public boolean equals(Object o) { |
if ( this == o) return true ; |
if (o == null || getClass() != o.getClass()) return false ; |
Customer customer = (Customer) o; |
if (name != null ? !name.equals(customer.name) : customer.name != null ) return false ; |
if (email != null ? !email.equals(customer.email) : customer.email != null ) return false ; |
return company != null ? company.equals(customer.company) : customer.company == null ; |
int result = name != null ? name.hashCode() : 0 ; |
result = 31 * result + (email != null ? email.hashCode() : 0 ); |
result = 31 * result + (company != null ? company.hashCode() : 0 ); |
public String toString() { |
", email='" + email + '\ '' + |
", company='" + company + '\ '' + |
Kotlin
data class Customer(var name: String, var email: String = "" , var company: String = "" ) |
As you can see yourself, There is a huge difference in number of lines of code between kotlin and java programming language to perform the same task. Single line in Kotlin is equivalent to entire class written in java. You will observe this difference many times if you are using Kotlin.
2. Safe
This language avoids entire classes of errors such as null pointer exceptions, the billion dollar mistakes. Let’s take an example –
There are 2 variables in the method. One can be null but other must not be null. Let’s see the example in both language
java
String neverNull = "something" ; |
if (neverNull.length > 0 ) { |
if (mightBeNull.length > 0 ) { |
Kotlin
var neverNull: String = "something" |
var mightBeNull: String? = null |
if (neverNull.length > 0 ) { |
if (mightBeNull.length > 0 ) { |
As you can see clearly, The Kotlin compiler enforces that variables that can hold null values are explicitly declared – thus no more NullPointerExceptions at runtime! You must declare explicitly if a value may be null.
3. Interoperable
Java and Kotlin are 100% compatible. You can also use any existing library on the JVM, including SAM support. You can use both languages in the same project. You can convert java code into Kotlin and vice-versa. You just need to target either JVM or Javascript. Write code in Kotlin and decide where you want to go. For example,
Both code has meaning.
Java
Button button = (Button) findViewById(R.id.button); |
button.setOnClickListener( new View.OnClickListener() { |
public void onClick(View view) { |
Kotlin
val button = findViewById(R.id.fab) as Button |
button.setOnClickListener { view -> } |
You can use any Java IDE to do programming in Kotlin. You can use IntelliJ, Android Studio or Eclipse etc. to develop application using Kotlin. You can also use command line to build application using Kotlin. You can use any way to code using Kotlin.

Use
Android Studio
Bundled with Studio 3.0, plugin available for earlier versions
Use
Eclipse
Install the plugin from the Eclipse Marketplace
Use
IntelliJ
Bundled with Community Edition or IntelliJ IDEA Ultimate
Use
Command Line
Use any editor and build from the command line