• Kotlin logo. Source: Wikipedia 2019.
    image
  • A project codebase can contain common and platform-specific modules. Source: Jemerov 2017.
    image
  • Kotlin is suitable for a range of application areas. Source: Gill 2017.
    image
  • Use of Kotlin data class simplifies coding. Source: Paul 2018.
    image
  • Survey from 2018 reveals favourite Kotlin features. Source: Pusher 2018.
    image

Kotlin (Language)

Improve this article. Show messages.

Summary

 image
Kotlin logo. Source: Wikipedia 2019.

Kotlin is a next-generation programming language developed by JetBrains. It combines both object-oriented and functional features. It's focused on interoperability, safety, clarity and tooling support. It can be used for server-side, desktop or mobile apps.

It's statically typed like Java, C, and C++. It's also fully compatible with Java but has a much simpler syntax. Google officially supports it for the Android platform and therefore Android apps can be written in Kotlin. Kotlin has multiplatform support, which means that code can be reused across Android, iOS or Web.

It's open sourced under Apache 2 License. It's managed by the Kotlin Foundation.

Milestones

Jul
2011

JetBrains unveils Kotlin at the JVM Language Summit. It's a new language for the Java Virtual Machine (JVM). The team is lead by Dmitry Jemerov and they have been working on it for almost a year. Since JetBrains makes development tools, work on IDE support for Kotlin happens in parallel with language design and evolution. JetBrains engineers later comment that although Scala was popular, it wasn't as fast or simple as desired. Groovy and Clojure had different programming paradigms.

Jan
2012

Dr. Dobb's Journal names Kotlin as the Language of the Month.

Feb
2012

JetBrains open sources the project under the Apache 2 License. This includes the compiler, Ant/Maven integration tools, IntelliJ IDEA plugin and enhancements to basic Java libraries.

Feb
2016

Kotlin v1.0 is released. Future releases will remain backward compatible with v1.0, thus showing that Kotlin is mature.

May
2017

At Google I/O 2017, Google announces support for Kotlin on Android. Until then, Android supported only Java and C++. It's mentioned that Expedia, Flipboard, Pinterest, and Square are among those already using Kotlin in production. Android support marks a turning point in Kotlin adoption. For example, by November 2017, number of lines of code on GitHub rose from 5 million to 25 millions.

Nov
2017
image

Kotlin v1.2 is released. In v1.1 it was possible to compile Kotlin to JavaScript and run within a browser. With v1.2, it's possible to share code between JVM and Javascript platforms. Regular updates to v1.2 are made. In September 2018, v1.2.70 is released.

Oct
2018

Kotlin v1.3 is released. Coroutines is now a stable feature and useful for writing non-blocking code. To compile code directly to native binaries, Kotlin/Native is released as beta version. To reuse code across platforms, multiplatform features remains experimental but gets many updates.

Nov
2018

Kotlin team releases version 1.0 of Ktor, a web framework built in Kotlin. It's good for building asynchronous servers and clients in connected systems. Thanks to coroutines, complex asynchronous constructs can be expressed as sequential code. It's claimed that Ktor is already running at scale in many production systems.

Discussion

  • In what application areas is Kotlin most suitable?
     image
    Kotlin is suitable for a range of application areas. Source: Gill 2017.

    Diverse set of apps can be built with Kotlin since it's a general-purpose language. It can be used to build mobile apps such as Android apps. Server-side apps can be built with Kotlin. Wherever Java is used for enterprise apps, such apps can be migrated to Kotlin. Since Kotlin can be compiled to JavaScript it can be used for web app development.

    Kotlin uses two file extensions: .kt for object-oriented programming and .kts for command-line scripting. This suggests that Kotlin can be used for scripting. Kotlin also has an interactive shell. This is useful for learning or clarifying doubts since results of code execution can be seen right away.

    In the real world, adopters of Kotlin include Pinterest, Gradle, Evernote, Corda, Coursera, Uber, Trello, Kickstarter, Square, and many more.

  • What's the difference between Kotlin and Java?
     image
    Use of Kotlin data class simplifies coding. Source: Paul 2018.

    While there are differences, it's important to first note that Kotlin is compatible with Java. Kotlin code can be mixed with Java and can call existing Java libraries. IDEs also support automated conversion of Java code to Kotlin code. This makes the transition from Java to Kotlin a lot easier for existing projects. Since Kotlin targets JVM, Kotlin .kt files compile to .jar files.

    Here are some reasons why it makes sense to move to Kotlin:

    • Learning Curve: Kotlin has a simpler syntax and therefore easier to learn.
    • Development Environment: Kotlin has excellent IDE support, including IntelliJ IDEA and Android Studio. Compilation is fast, sometimes faster than Java. Similar support is lacking in Scala.
    • Concise: Kotlin does more with less code. Strong type inference is one reason. Java has always been known for its verbosity.
    • Performance: With Kotlin’s support for inline functions, code often runs faster.
    • Safe: Kotlin avoids NullPointerException. Since you write less code, there's less chance of introducing errors.
    • Multiplatform: Though initially created for JVM, Kotlin can be used for front-end development and even native apps.
  • Could you mention some key features of Kotlin?
     image
    Survey from 2018 reveals favourite Kotlin features. Source: Pusher 2018.

    Here are some features of Kotlin:

    • Extensions: Add extra functionality to an existing component.
    • Higher-order Functions: Functions themselves can be arguments to or returned from other functions.
    • Interoperable with Java: Kotlin code can be called from Java code and vice versa.
    • Null Safety: Objects are null safe by default so that nasty null pointer exceptions are avoided.
    • Smart Cast: Object is automatically cast to a desired type.
    • Default Arguments: Java took the approach of function overloading instead.
    • Named Arguments: Makes code more readable and order of arguments can be changed.
    • Multi-value Returns: Returned values can be unpacked into separate variables using destructuring declaration.
    • Data Class: Gets rid of the more verbose getters and setters in Java.

    A JetBrains survey from 2017 showed that three features are highly requested: collection literals, Single Abstract Method (SAM) conversions for Kotlin interfaces, and truly immutable data. JetBrains makes no promise if and when these features might be available. There are also features that developers don't want: accessing private members from tests, collection comprehensions, and static members in classes.

  • What resources are available for a Kotlin beginner?
    Android folks talk about the good things in Kotlin. Source: Android Developers YouTube 2017.

    Start by reading some examples in Kotlin Playground. For more in-depth information, read the official Kotlin documentation or tutorials. To learn by solving short exercises, Kotlin Koans is recommended. It's a series of exercises that will help you learn Kotlin step by step.

    Kotlin style guide is an essential read. There's also a Kotlin for Android style guide.

    For latest news, follow the Kotlin blog or the weekly mailing list. Links to other community resources such as podcasts, talks and Slack channels are available online.

Sample Code

  • // Source: https://play.kotlinlang.org/byExample/overview
    // Accessed: 2019-01-18
     
     
    // Hello world
    fun main() {
        println("Hello, World!")
    }
     
     
    // Function example with default parameter values
    fun printMessageWithPrefix(message: String, prefix: String = "Info") {
        println("[$prefix] $message")
    }
     
     
    // Call function with named arguments
    printMessageWithPrefix(prefix = "Intro", message = "Hello world!")
     
     
    // Function that returns an integer
    fun sum(x: Int, y: Int): Int {
        return x + y
    }
     
     
    // Single-expression function: integer return is inferred
    fun multiply(x: Int, y: Int) = x * y
     
     
    // Use val for immutability
    var a: String = "initial"
    val b: Int = 1
     
     
    // Null safety
    var neverNull: String = "This can't be null"
    neverNull = null // this results in a compiler error
    var nullable: String? = "You can keep a null here"
    nullable = null // no error here
     
     
    // Use of classes
    class Customer
     
    class Contact(val id: Int, var email: String)
     
    fun main() {
        val customer = Customer()
        val contact = Contact(1, "This email address is being protected from spambots. You need JavaScript enabled to view it.")
        println(contact.id)
        contact.email = "This email address is being protected from spambots. You need JavaScript enabled to view it."
    }
     
     
    // Inheritance: use open to allow inheritance
    open class Tiger(val origin: String) {
        fun sayHello() {
            println("A tiger from $origin says: grrhhh!")
        }
    }
     
    class SiberianTiger : Tiger("Siberia")
     
    fun main() {
        val tiger: Tiger = SiberianTiger()
        tiger.sayHello()
    }
     
     
    // for loop
    val cakes = listOf("carrot", "cheese", "chocolate")
     
    for (cake in cakes) {
        println("Yummy, it's a $cake cake!")
    }
     
     
    // for loop based on range
    for(i in 2..8 step 2) {
        print(i)
    }
     
     
    // Higher-order functions
    fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
        return operation(x, y)
    }
     
    fun sum(x: Int, y: Int) = x + y
     
    fun main() {
        val sumResult = calculate(4, 5, ::sum) // :: references a function by name
        val mulResult = calculate(4, 5) { a, b -> a * b } // lambda function
        println("sumResult $sumResult, mulResult $mulResult")
    }

References

  1. Android Developers YouTube. 2017. "Kotlin: Android Support Announced at Google I/O." June 08. Accessed 2019-01-20.
  2. Belov, Roman. 2018a. "Kotlin 1.3 Released with Coroutines, Kotlin/Native Beta, and more." Kotlin Blog, JetBrains, October 29. Accessed 2019-01-20.
  3. Belov, Roman. 2018b. "Ktor 1.0 Released: A Connected Applications Framework by JetBrains." Kotlin Blog, JetBrains, November 19. Accessed 2019-01-18.
  4. Breslav, Andrey. 2016. "Kotlin 1.0 Released: Pragmatic Language for JVM and Android." Kotlin Blog, JetBrains, February 15. Accessed 2019-01-20.
  5. Breslav, Andrey. 2017. "Kotlin Future Features Survey Results." Kotlin Blog, JetBrains, June 13. Accessed 2019-01-20.
  6. Cleron, Mike. 2017. "Android Announces Support for Kotlin." Android Developers Blog, May 17. Accessed 2019-01-20.
  7. Finley, Klint. 2017. "Kotlin: the Upstart Coding Language Conquering Silicon Valley." Wired, July 18. Accessed 2019-01-18.
  8. Gill, Navdeep Singh. 2017. "Overview of Kotlin and Comparison Between Kotlin and Java." XenonStack Blog, May 17. Accessed 2019-01-18.
  9. Heiss, Janice J. 2013. "The Advent of Kotlin: A Conversation with JetBrains' Andrey Breslav." Oracle Technology Network, Oracle, April. Accessed 2019-01-18.
  10. Igushkin, Sergey. 2018. "Kotlin 1.2.70 is Out!" Kotlin Blog, JetBrains, September 13. Accessed 2019-01-20.
  11. Jemerov, Dmitry. 2011. "Hello World." Kotlin Blog, JetBrains, July 19. Accessed 2019-01-20.
  12. Jemerov, Dmitry. 2017. "Kotlin 1.2 Released: Sharing Code between Platforms." Kotlin Blog, JetBrains, November 28. Accessed 2019-01-20.
  13. Kotlin. 2019a. "Community Overview." Kotlin. Accessed 2019-01-18.
  14. Kotlin. 2019b. "Kotlin Foundation." Kotlin. Accessed 2019-01-20.
  15. Kotlin Docs. 2018. "Working with the Command Line Compiler." October 01. Accessed 2019-01-20.
  16. Leiva, Antonio. 2017. "12 reasons why you should start using Kotlin for Android today (KAD 30)." July 11. Accessed 2019-01-20.
  17. Oreshnikova, Ann. 2012. "Kotlin Goes Open Source!" Kotlin Blog, JetBrains, February 14. Accessed 2019-01-20.
  18. Paul, Javin. 2018. "Top 5 Kotlin Programming Courses for Java and Android Programmers." Hacker Noon, via Medium, October 21. Accessed 2019-01-18.
  19. Pusher. 2018. "The State of Kotlin 2018." Pusher. Accessed 2019-01-18.
  20. Rai, Pankaj. 2017. "The best features of Kotlin!" AndroidPub, via Medium, May 20. Accessed 2019-01-18.
  21. Sharma, Avinash. 2018. "Know Why These 14 Famous Apps Migrated from Java to Kotlin." Blog, AppInventiv Technologies, June 13. Accessed 2019-01-20.
  22. Titus, Jason. 2017. "Google I/O 2017: Empowering developers to build the best experiences across platforms." Android Developers Blog, May 17. Accessed 2019-01-20.
  23. Wikipedia. 2019. "Kotlin (programming language)." Wikipedia, January 12. Accessed 2019-01-18.

Milestones

Jul
2011

JetBrains unveils Kotlin at the JVM Language Summit. It's a new language for the Java Virtual Machine (JVM). The team is lead by Dmitry Jemerov and they have been working on it for almost a year. Since JetBrains makes development tools, work on IDE support for Kotlin happens in parallel with language design and evolution. JetBrains engineers later comment that although Scala was popular, it wasn't as fast or simple as desired. Groovy and Clojure had different programming paradigms.

Jan
2012

Dr. Dobb's Journal names Kotlin as the Language of the Month.

Feb
2012

JetBrains open sources the project under the Apache 2 License. This includes the compiler, Ant/Maven integration tools, IntelliJ IDEA plugin and enhancements to basic Java libraries.

Feb
2016

Kotlin v1.0 is released. Future releases will remain backward compatible with v1.0, thus showing that Kotlin is mature.

May
2017

At Google I/O 2017, Google announces support for Kotlin on Android. Until then, Android supported only Java and C++. It's mentioned that Expedia, Flipboard, Pinterest, and Square are among those already using Kotlin in production. Android support marks a turning point in Kotlin adoption. For example, by November 2017, number of lines of code on GitHub rose from 5 million to 25 millions.

Nov
2017
image

Kotlin v1.2 is released. In v1.1 it was possible to compile Kotlin to JavaScript and run within a browser. With v1.2, it's possible to share code between JVM and Javascript platforms. Regular updates to v1.2 are made. In September 2018, v1.2.70 is released.

Oct
2018

Kotlin v1.3 is released. Coroutines is now a stable feature and useful for writing non-blocking code. To compile code directly to native binaries, Kotlin/Native is released as beta version. To reuse code across platforms, multiplatform features remains experimental but gets many updates.

Nov
2018

Kotlin team releases version 1.0 of Ktor, a web framework built in Kotlin. It's good for building asynchronous servers and clients in connected systems. Thanks to coroutines, complex asynchronous constructs can be expressed as sequential code. It's claimed that Ktor is already running at scale in many production systems.

Tags

See Also

Further Reading

  1. Saini, Ajay. 2017. "How to use Kotlin in Android Studio." Medium, May 18. Accessed 2019-01-20.
  2. Finley, Klint. 2017. "Kotlin: the Upstart Coding Language Conquering Silicon Valley." Wired, July 18. Accessed 2019-01-18.
  3. Heiss, Janice J. 2013. "The Advent of Kotlin: A Conversation with JetBrains' Andrey Breslav." Oracle Technology Network, Oracle, April. Accessed 2019-01-18.
  4. Zukanov, Vasiliy. 2017. "Kotlin vs Java The Whole Story." TechYourChance, September 07. Accessed 2019-01-18.
  5. Rai, Pankaj. 2017. "The best features of Kotlin!" AndroidPub, via Medium, May 20. Accessed 2019-01-18.
  6. Pusher. 2018. "The State of Kotlin 2018." Pusher. Accessed 2019-01-18.

Top Contributors

Last update: 2019-01-20 08:25:33 by arvindpdmn
Creation: 2018-09-15 10:05:42 by GuruBhat

Article Stats

Author-wise Stats for Article Edits

Author
No. of Edits
No. of Chats
DevCoins
4
1
788
2
0
181
1109
Words
1
Chats
6
Edits
2
Likes
472
Hits

Cite As

Devopedia. 2019. "Kotlin (Language)." Version 6, January 20. Accessed 2019-04-20. https://devopedia.org/kotlin-language