Open In App

Introduction to Kotlin

Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and a new language for the JVM. Kotlin is object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code.

Kotlin is sponsored by Google, announced as one of the official languages for Android Development in 2017.

Example of Kotlin –




fun main()
{
    println("Hello Geeks");
}


Key Features of Kotlin:

  1. Statically typed – Statically typed is a programming language characteristic that means the type of every variable and expression is known at compile time. Although it is statically typed language, it does not require you to explicitly specify the type of every variable you declare.
  2. Data Classes– In Kotlin, there are Data Classes which lead to auto-generation of boilerplate like equals, hashCode, toString, getters/setters and much more.

    Consider the following example –




    /*     Java Code     */
    class Book {
        private String title;
        private Author author;
        public String getTitle()
        {
            return title;
        }
        public void setTitle(String title)
        {
            this.title = title;
        }
        public Author getAuthor()
        {
            return author;
        }
        public void setAuthor(Author author)
        {
            this.author = author;
        }
    }

    
    

    But in Kotlin only one line used to define the above class –

    /* Kotlin Code */
    data class Book(var title:String, var author:Author)
    
  3. Concise – It drastically reduces the extra code written in other object-oriented programming languages.
  4. Safe – It provides the safety from most annoying and irritating NullPointerExceptions by supporting nullability as part of its system.
    Every variable in Kotlin is non-null by default.

    String s = "Hello Geeks"    // Non-null 
    

    If we try to assign s null value then it gives compile time error.So,

    s = null                    // compile time error
    

    To assign null value to any string string it should be declared as nullable.

    String nullableStr? = null  // compiles successfully
    

    length() function also disabled on the nullable strings.

  5. Interoperable with Java – Kotlin runs on Java Virtual Machine(JVM) so it is totally interoperable with java. We can easily access use java code from kotlin and kotlin code from java.
  6. Functional and Object Oriented Capabilities – Kotlin has rich set of many useful methods which includes higher-order functions, lambda expressions, operator overloading, lazy evaluation, operator overloading and much more.
    Higher order function is a function which accepts function as a parameter or returns a function or can do both.

    Example of higher-order function –

    fun myFun(company: String,product: String, fn: (String,String) -> String): Unit {
        val result = fn(company,product)
        println(result)
    }
    
    fun main(args: Array){
        val fn:(String,String)->String={org,portal->"$org develops $portal"}
        myFun("JetBrains","Kotlin",fn)
    }
    

    Output:

    JetBrains develops Kotlin
  7. Smart Cast – It explicitly typecasts the immutable values and inserts the value in its safe cast automatically.
    If we try to access a nullable type of String ( String? = “BYE”) without safe cast it will generate a compile error.

    fun main(args: Array){
        var string: String? = "BYE"          
            print(string.length)       // compile time error
        }
    }
    
    fun main(args: Array){
        var string: String? = "BYE"
        if(string != null) {               // smart cast
            print(string.length) 
        }
    }
    
  8. Compilation time – It has higher performance and fast compilation time.
  9. Tool- Friendly – It has excellent tooling support. Any of the Java IDEs – IntelliJ IDEA, Eclipse and Android Studio can be used for Kotlin. We can also be run Kotlin program from command line.

Advantages of Kotlin language:

  • Easy to learn – Basic is almost similar to java.If anybody worked in java then easily understand in no time.
  • Kotlin is multi-platform – Kotlin is supported by all IDEs of java so you can write your program and execute them on any machine which supports JVM.
  • It’s much safer than Java.
  • It allows using the Java frameworks and libraries in your new Kotlin projects by using advanced frameworks without any need to change the whole project in Java.
  • Kotlin programming language, including the compiler, libraries and all the tooling is completely free and open source and available on github. Here is the link for Github https://github.com/JetBrains/kotlin

Applications of Kotlin language:

  • You can use Kotlin to build Android Application.
  • Kotlin can also compile to JavaScript, and making it available for the frontend.
  • It is also designed to work well for web development and server-side development.


Last Updated : 06 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads