Welcome to this course dedicated to Kotlin, the new programming language designed for Java development platforms.
The objective of this course is to introduce you to Kotlin step by step, so that you can appreciate (or not… ) all the possibilities that this language offers compared to Java.
What? But I don’t understand—why are we comparing Kotlin to Java?
Simply because Kotlin and Java are 100% interoperable: this means that you can write source code in Java AND Kotlin in the same project. You can call methods created in Kotlin in Java, and vice versa. Pretty cool, huh?
The Origins of Kotlin
Let’s go back to the dawn of time… well, almost! In fact, the idea of Kotlin was born in 2010 in the minds of the engineers at JetBrains (the famous creators of the IntelliJ IDE, which is the basis for Android Studio).
These engineers, seeing the design limitations that Java imposed on their various products, started to look for a viable and long-term alternative language with one requirement: it must be 100% compatible with existing Java code.
Indeed, it was impossible for JetBrains to start from scratch on all their existing Java projects, given how massive they are (we’re talking about several million lines of code!).
After unsuccessfully trying other JVM-compatible languages such as Scala (its compilation time was too long), JetBrains engineers took on a formidable challenge… creating their own language!
So, five years later, the first stable version of Kotlin (1.0) was released, available as open source on GitHub.
The Kotlin Philosophy
Every worthwhile project has a philosophy or something it wants to accomplish; Kotlin is no exception to this rule. Kotlin is concise, safe, pragmatic, and 100% interoperable with Java.
With Kotlin, you will be more productive and level-headed in coping with certain types of recurring errors in Java, like the infamous “NullPointerException.” Thanks to its almost-perfect compatibility with Java, Kotlin can be used to create native mobile applications on Android and web applications (J2EE), for example.
All this to say, wherever you can use Java, you can use Kotlin.
Well, that’s great! But how does it work under the hood, so to speak?
Good question! When you develop in Java, your class (.java) is compiled by the Java compiler (present in your IDE) which will transform it into a file (.class) containing your compiled code (the famous bytecode).
With Kotlin, the same thing happens, except that the code to be compiled is written in Kotlin, not Java. However, the code compiled by the Kotlin compiler (kotlinc) will remain exactly the same (bytecode)!
A Statically-Typed Language
Like Java, Kotlin is a statically-typed programming language.
Hmm, okay… This means that we can use the static keyword, right?
Wrong, that was a trap! Actually, “statically-typed” means that the type (Integer, String, Boolean, etc.) of each variable in a program is known at compile time. This is the process that allows your IDE to show you a nice red error when you try to add a number to a string, for example…
It’s better that our IDE alerts us right away than our application crashing later, right?
This type of language differs from dynamically-typed languages, in which you can define variables or functions that store or return data of any type, which will be resolved only when your program is run. This type of language has the advantage of accelerating development of a program because the produced code is shorter. Conversely, it will often require more memory at runtime to resolve the variable types in real time.
Aha, I get it! But why are you telling us all this?
Only because the JetBrains developers were inspired to integrate dynamic typing into Kotlin to improve its readability, even though Kotlin is statically typed.
To give you an example:
val zero = 0val message = "Hello, world!"fun sayHello() = "Hello, everyone!"
Beautiful, isn’t it? In fact, everything will be done by the Kotlin compiler, which will automatically detect the type of each of your variables. This is called type inference. Don’t worry, we’ll talk about this in detail in the next chapter.
To sum it all up, a statically-typed language such as Kotlin (or Java) has several major advantages, such as:
Performance: Since the types are known at compile time, there is no need to infer them at run time.
Reliability: The compiler shows you errors at compile time, minimizing the chances that your application crashes during execution.
A Functional Language
Since you have been using Java, you should be used to object-oriented programming. Kotlin is also an object-oriented language, but with a small difference: it is also a functional language.
I’m guessing that does NOT mean a language that... functions?
No, indeed! Here are some elements that characterize a functional programming language:
First-class function: This means that we can use functions as values in our code. We can, for example, store functions in variables, pass functions as parameters, or return functions! We will see this in more detail in the chapter dealing with lambdas…
Immutability: The functional approach encourages the use of “immutable” variables, i.e., their value cannot change after they’ve been created (i.e., once the variable has been created and instantiated, we cannot modify its content). This technique can improve the reliability and the performance of a program, by making it resistant to problems related to multi-threading, such as simultaneous access and modification of the same variable in different threads. We will deal with this phenomenon in the next chapter.
No side effects: By only using pure functions, we make sure that the result will always be the same. No matter how many times the function is called, it will only modify its own local variables, and therefore will have no unwanted (or unexpected) effect on other global variables in your application. We will discuss this in more detail in the chapter on functions.
So, are you starting to get a feel for the power and flexibility of Kotlin? Well, hold on tight to your keyboard, the sequel is coming in the next chapter!
Kotlin was developed by JetBrains in order to overcome the limitations imposed by Java in the development of its various products.
Kotlin is a programming language that is concise, safe, pragmatic, and 100% interoperable with Java.
Kotlin is a statically-typed programming language, but it does support type inference thanks to its compiler.
Kotlin supports both object-oriented and functional programming.
Kotlin is open-source, supports most IDEs, and runs on most current Java platforms (where JVM bytecode is accepted).
Now that you know the history of Kotlin, let’s discover how to declare and initialize variables in the next chapter!