Kotlin

Kotlin Overview

In this article, I’ll tell you gives Kotlin Overview. Android discuss all advantages of Kotlin over Java in Android for Android Development. I will tell you also why Kotlin is more powerful than Java.

Kotlin

In Google I/O 2017 the Kotlin has been announced as the official Android language.  In recent years the Kotlin has gained tremendous popularity in terms of developing android applications. Kotlin language has been developed by JetBrains company, that has created the IntelliJ IDEA and if you are an Android developer then you must be knowing that the Android studio has been built on top of the IntelliJ IDEA. So you can understand the importance of the JetBrains company.  It is the creator of IntelliJ IDEA and also the Kotlin language.

Kotlin Highlights

Kotlin language is actually the open source project primarily under the Apache2 license. Which again promotes the developer ecosystem as it is open source and it makes the developer’s community united and contribute to this open source project more freely.

Is it meant to replace Java?

In your teammates always discuss this question in general discussion. You can we say it just because in Google IO 2017 the Kotlin has been announced as an official language of Android so is it going to replace Java in future. In my opinion, the answers are NOT, because the maximum number of Android projects at present has been developed in Java. So it will not be going to replace

Why Kotlin?

Kotlin is more powerful than Java. It more expressive, concise and multiplatform programming language. Kotlin also provides the safety features for your application such as it is immutable in nature and nullability. These two features make your app more healthy and safe. Kotlin ensures more performance of your application.

I have written separate articles for Why use Kotlin for Android Development? I recommend to read it It gives you more clarity on that.

Feature of Kotlin

1. It’s a JVM language

Java is the first JVM language so similar to Java the Kotlin is also the JVM language so you need to install Java JDK that contains the Java Virtual Machine before you start developing any applications. Let’s see below

kotlin overview
2. Expressive, Concise

Kotlin is more expensive and concise in the less amount of code you can get more output compared to Java where you have to write a large number of lines of code to express the same thing so we can say Kotlin is actually more than size than Java in the least number of lines.

Kotlin avoids all boilerplate code, suppose if you want to write a POJO class, Java takes 50 lines of code for this class, in Kotlin you can achieve in fewer lines. See below code snippets and switch tab.

import com.google.gson.annotations.SerializedName
data class Movie (

	@SerializedName("Title") val title : String,
	@SerializedName("Year") val year : String,
	@SerializedName("imdbID") val imdbID : String,
	@SerializedName("Type") val type : String,
	@SerializedName("Poster") val poster : String
)
import com.google.gson.annotations.SerializedName;

public class Movie {

  @SerializedName("imdbID")
  private String id;
  @SerializedName("Poster")
  private String poster;
  @SerializedName("Title")
  private String title;
  @SerializedName("Type")
  private String type;
  @SerializedName("Year")
  private String year;

  public String getImdbID() {
    return id;
  }

  public void setImdbID(String imdbID) {
    id = imdbID;
  }

  public String getPoster() {
    return poster;
  }

  public void setPoster(String mPoster) {
    poster = mPoster;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    title = title;
  }

  public String getType() {
    return type;
  }

  public void setType(String type) {
    type = type;
  }

  public String getYear() {
    return year;
  }

  public void setYear(String year) {
    year = year;
  }
}
3. Supported high order functions

It’s simply supported high order functions now in case of Java we can only pass variable or reference variable to the functions ride but in case of Kotlin we can simply pass functions within a function that is a path from variable we can also pass function as a parameter

4. Lambda Expression

The Kotlin also supports the lambda expression

  val product = { a: Int, b: Int -> a * b }
  val result = product(9, 3)
  println(result)
5. Strings Templates

Strings Template awesome feature of Kotlin. That allows strings to contain template expressions. String Template expression starts with a dollar($) sign

var message = "Page Number = $pageNo"
String message = "Page Number = " + pageNo;
6. Kotlin and Java are together interoperable

 The most important feature is that Kotlin and Java are together interoperable. Now, what does this interoperable means? It simply means that inside the same application you can have both Java and Kotlin file.

Supposed you are making an application and you can have Kotlin programming code as well along with the Java programming codes. If currently your application is built on top of Java then please don’t worry about it you can add Kotlin codes in the same Java project.

 The question arises if we have both Java and Kotlin files within the same project then how can they interact now this is the beauty of using Kotlin and Java because there interoperable so you can call the Java functions from the Kotlin file and vice-versa that is you can also call the Kotlin file from the Java files.

Java class using in Kotlin class example

    
    public class Movie {

        private String name;

        public Movie(String s)
        {
            name = s;
        }

        public String getName()
        {
            return name;
        }

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

        public void bookMovie()
        {
            System.out.println("You booked movie ticket -  " + name);
        }
    }

Access Java class in Kotlin

    val movie = Movie("avengers endgame")
    println(movie.name)
    println(customer.bookMovie())

Thank’s for reading this articles, Upcoming articles I’ll also explain each feature and Kotlin fundamentals in details.

Leave a Reply

2000