dcsimg
 

Introduction to Kotlin

by Brad Jones
Introduction to Kotlin

Kotlin has been gaining steam in the developer community over the last few years. Here’s a quick glance at what Kotlin has to offer.

As developers, it is good to be aware of the other programming languages and tools that are available for you to use. If you are a Java developer, one such language you should be aware of is Kotlin. Kotlin is a programming language created by open-source contributors with JetBrains that is statically typed, general purpose and related to Java. Kotlin can be used to create cross-platform mobile applications as well as native, Android, Web, server-side and other applications.

While Kotlin is only in version 1.5.10 as of the time this article was written, it has actually been around since about July 2011, roughly nine years. It started to see a bigger uptick in adoption in 2017, around the time Google announced it as an official language for Android development.

Kotlin is said to have a number of advantages over other programming languages. These factors include:

  • Concise
  • Efficiency
  • Enhanced run-time performance
  • Familiar tools to Java
  • Statically typed
  • Safety
  • Interoperability
  • Built-in null safety
  • High-order functions
  • Smart casts
  • Coroutine feature
  • Support for functional programming

Hello World in Kotlin 

While Kotlin can work with existing Java libraries, frameworks, and the JVM, it is its own programming language. As with any programming language, most developers want to see the standard Hello world application to see what is different. The following is a complete Hello World Kotlin app:

fun main() {
    println("Hello, World")
}

As you can see, this looks pretty straightforward, with the standard starting point of a main() function and the use of a printing method, println(). The function is started with the Kotlin keyword, fun. If you wanted to use command line arguments or define a simple variable, you could adjust this listing as such:

fun main(args: Array) {
   val msg = “Hello, World!”
   println($msg)
   println(“The command line arguments are:”)
    for (arg in args) {
        println(arg)
    }
}

You can see in this simple listing that the arguments are captured as an Array of Strings, which are then displayed using an enhanced for loop. The listing also declares a simple variable called message (msg) that is assigned a string that is displayed. While slightly different from Java, overall, it should still look familiar.

Kotlin Native Support

It is worth pointing out that Kotlin native support is available for a variety of platforms, including iOS, macOS, watchOS, tvOS, Android, Windows, Linux, and WebAssembly. You can also create multi-platform projects to make it easier to share code while targeting different operating systems.

Getting Started with Kotlin

Of course, seeing the code for a Hello Kotlin application isn’t the same as running the code. You can obtain the Kotlin native compiler as part of the standard Kotlin distribution, which can be downloaded from GitHub at the following URL:

https://github.com/JetBrains/kotlin/releases/tag/v1.4.10

This release supports Linux, MacOS, and Windows and should be all you need to get started with your first Kotlin application today!

Of course, to get a better feel for a programming language, a little more code needs to be presented. To that end, the following listing helps to illustrate a few features of Kotlin, including the ability to implement an iterator operator within a class as well as to show how it can loop through a List. If you are familiar with Java, this code should be easy to follow as it simply creates a list of two teachers that it then displays.

class Teacher(val name: String)

class School(val teachers: List) {

   operator fun iterator(): Iterator {
      return teachers.iterator()
   }
}

fun main() {

   val school = School(listOf(Teacher("Mrs. Jones"), Teacher("Mr. Smith")))

   for (teacher in school) {
      println("Great teachers include ${teacher.name}")
   }
}

The output from this listing should be:

 
Great teachers include Mrs. Jones
Great teachers include Mr. Smith
This article was originally published on Friday Oct 9th 2020
Home
Mobile Site | Full Site