Kotlin Tutorial | Introduction to Kotlin Programming language

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 class Customer {
   private String name;
   private String email;
   private String company;

   public Customer(String name) {
       this(name, "", "");
   }

   public Customer(String name, String email) {
       this(name, email, "");

   }

   public Customer(String name, String email, String company) {
       this.name = name;
       this.email = email;
       this.company = company;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getEmail() {
       return email;
   }

   public void setEmail(String email) {
       this.email = email;
   }

   public String getCompany() {
       return company;
   }

   public void setCompany(String company) {
       this.company = company;
   }

   @Override
   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;
   }

   @Override
   public int hashCode() {
       int result = name != null ? name.hashCode() : 0;
       result = 31 * result + (email != null ? email.hashCode() : 0);
       result = 31 * result + (company != null ? company.hashCode() : 0);
       return result;
   }

   @Override
   public String toString() {
       return "Customer{" +
               "name='" + name + '\'' +
               ", 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";
String mayBeNull = null;
if (neverNull.length > 0) {   // This is OK...
   // write something here...
}

if (mightBeNull.length > 0) { // This is OK...
   // write something here...
}
Kotlin
var neverNull: String = "something"
var mightBeNull: String? = null // "?" indicates this can be null

if (neverNull.length > 0) {   // This is OK
   // write something here...
}

if (mightBeNull.length > 0) { // Compiler catches this error for you
   // write something here...
}

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() { 
 @Override
 public void onClick(View view) {
 /* your code */
 }
});
Kotlin
val button = findViewById(R.id.fab) as Button 
button.setOnClickListener { view -> /* your code */}



4. Tool-Friendly

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.

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

Leave a Reply