Introduction of Kotlin programming language

It’s a new programming language targeting the Java platform. Kotlin is concise, safe, pragmatic, and focused on interoperability with Java code. It can be used almost everywhere Java is used today – for server-side development, Android apps, and much more. Kotlin works great with all existing Java libraries and frameworks and runs with the same level of performance as Java

Target platforms: server-side, Android, anywhere Java runs

The primary goal of Kotlin is to provide a more concise, more productive, safer alternative to Java that’s suitable in all contexts where Java is used today. Java is an extremely popular language, and it’s used in a broad variety of environments, from smart cards (Java Card technology) to the largest data centers run by Google, Twitter, LinkedIn, and other internet-scale companies. In most of these places, using Kotlin can help developers achieve their goals with less code and fewer annoyances along the way.

Kotlin build process

Kotlin build process
Kotlin build process

The most common areas to use Kotlin are:

  • Building server-side code (typically, backends of web applications)
  • Building mobile applications that run on Android devices

But Kotlin works in other contexts as well. For example, you can use the Intel Multi-OS
Engine (https://software.intel.com/en-us/multi-os-engine) to run Kotlin code on iOS
devices. To build desktop applications, you can use Kotlin together with TornadoFX
(https://github.com/edvin/tornadofx) and JavaFX.

In addition to Java, Kotlin can be compiled to JavaScript, allowing you to run Kotlin code in the browser. But as of this writing, JavaScript support is still being explored and prototyped at JetBrains, so it’s out of scope for this book. Other platforms are also under consideration for future versions of the language

Statically typed

Just like Java, Kotlin is a statically typed programming language. This means the type of every expression in a program is known at compile time, and the compiler can validate that the methods and fields you’re trying to access exist on the objects you’re using

On the other hand, in contrast to Java, Kotlin doesn’t require you to specify the type of every variable explicitly in your source code. In many cases, the type of a variable can automatically be determined from the context, allowing you to omit the type declaration. Here’s the simplest possible example of this:

val x = 1

You’re declaring a variable, and because it’s initialized with an integer value, Kotlin automatically determines that its type is Int. The ability of the compiler to determine types from context is called type inference

Following are some of the benefits of static typing:

  • Performance—Calling methods is faster because there’s no need to figure out at runtime which method needs to be called.
  • Reliability—The compiler verifies the correctness of the program, so there are fewer chances for crashes at runtime
  • Maintainability—Working with unfamiliar code is easier because you can see what kind of objects the code is working with
  • Tool support—Static typing enables reliable refactorings, precise code completion, and other IDE features

Functional and object-oriented

The key concepts of functional programming are as follows

  • First-class functions—You work with functions (pieces of behavior) as values. You can store them in variables, pass them as parameters, or return them from other functions
  • Immutability—You work with immutable objects, which guarantees that their state can’t change after their creation
  • No side effects—You use pure functions that return the same result given the same inputs and don’t modify the state of other objects or interact with the outside world

The second benefit of functional code is safe multithreading. One of the biggest sources of errors in multithreaded programs is modification of the same data from multiple threads without proper synchronization. If you use immutable data structures and pure functions, you can be sure that such unsafe modifications won’t happen, and you don’t need to come up with complicated synchronization schemes.

Kotlin on the server side

Server-side programming is a fairly broad concept. It encompasses all of the following types of applications and much more:

  • Web applications that return HTML pages to a browser
  • Backends of mobile applications that expose a JSON API over HTTP
  • Microservices that communicate with other microservices over an RPC protocol

One of the simplest use cases for that feature is an HTML generation library, which can replace an external template language with a concise and fully type-safe solution. Here’s an example:

fun renderPersonList(persons: Collection<Person>) =
createHTML().table {
for (person in persons) {
tr {
td { +person.name }
td { +person.age }
}
}
}
}

Kotlin on Android

A typical mobile application is much different from a typical enterprise application. It’s much smaller, it’s less dependent on integration with existing codebases, and it usually needs to be delivered quickly while ensuring reliable operation on a large variety of devices. Kotlin works just as well for projects of that kind.

Interoperable

Regarding interoperability, your first concern probably is, “Can I use my existing libraries?” With Kotlin, the answer is, “Yes, absolutely.” Regardless of the kind of APIs the library requires you to use, you can work with them from Kotlin. You can call Java methods, extend Java classes and implement interfaces, apply Java annotations to your Kotlin classes, and so on.

Unlike some other JVM languages, Kotlin goes even further with interoperability, making it effortless to call Kotlin code from Java as well. No tricks are required: Kotlin classes and methods can be called exactly like regular Java classes and methods. This gives you the ultimate flexibility in mixing Java and Kotlin code anywhere in your project. When you start adopting Kotlin in your Java project, you can run the Java-to-Kotlin converter on any single class in your codebase, and the rest of the code will continue to compile and work without any modifications. This works regardless of the role of the class you’ve converted.

Leave a Comment