The Kotlin programming language is a modern language that gives you more power for your everyday tasks. 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 is 100% compatible with all existing Java frameworks, and has good tooling support. It’s a pragmatic language with a very low learning curve, and can be quickly grasped by Java developers. Kotlin code might be compiled not only to JVM bytecode but to JavaScript and Native code as well, but this course is focused on Kotlin/JVM.
This course aims to share with you the power and the beauty of Kotlin. We'll have a basic overview of the language, as well as a discussion of many corner cases, especially concerning Java interoperability. The course is based on your Java experience; it shows the similarities between the two languages and focuses on what's going to be different.
Note that this course won't cover the programming fundamentals. We'll discuss: basic syntax, nullability, functional programming with Kotlin, object-oriented programming with Kotlin, the power of the Kotlin standard library, and Java interoperability.

講師

Svetlana Isakova

Andrey Breslav

字幕

It's really easy to start using Kotlin, especially if you're a Java developer. You can use all the existing Java frameworks and libraries from Kotlin. You can even mix Kotlin and Java code in one project. Kotlin is a JVM language, so you can easily call Java code from Kotlin, but that also works in the other direction. Kotlin is designed in a way so that using its all features from Java was straightforward. That means, that you can make the transition from Java to Kotlin very slow by adding new features to your existing Java application written in Kotlin. What do you think: is the Kotlin code compiled to Java source code or Java bytecode? Of course, it's compiled to Java bytecode; compiling it to Java source code first would be really ineffective. There is a powerful tool that helps to speed up the process of learning language based on your Java experience. This tool is automatic Java to Kotlin converter. The converter has two main use cases. Learning the language and incorporating it into an existing Java application. When you know how to write something in Java, but don't yet know or remember the exact construct in Kotlin, you can write it in Java first and then convert it. It's also extremely useful to use the converter when you start adding Kotlin into existing Java application. When you have a Java class and you need to modify it, you may prefer using Kotlin. In this case, you can convert this Java class to Kotlin first and then you'll be able to write new code in Kotlin. Now let's look at Java to Kotlin converter in action. This typical example of a Java class is often demonstrated to show differences between Java and modern languages like Kotlin. This Java class does nothing meaningful, it just stores the data. The ‘Person’ class contains two fields, constructor and two getters. We can convert this code automatically into Kotlin using “Java to Kotlin” converter. You can call an action “Convert Java file to Kotlin file” in IntelliJ IDEA or in Android studio. As a result, it gives us the same code written in Kotlin. You can see the difference: it's much more simple and concise. Under the hood, at the bytecode level, the same constructor and two getters are generated. So from Java point of view, that’s the same class. What’s more, you can add a 'data' modifier to this class. That will automatically generate several additional methods for it. Note that we’ll discuss the syntax for property declaration in detail later (in this example it’s ‘val name’ and ‘val age’), as well as the full syntax for variable and function declarations. For now, I want to illustrate how the automatic converter works, and highlight some differences with Java. That’s how we can use the ‘Person’ class. We create an instance of 'Person', and then we print its name. At first, you see the code written in Java, then the analogous code in Kotlin. The main point to note here is that there is no 'new' keyword in Kotlin. You simply call the constructor as a regular function without 'new' keyword. Let’s now look at another example of Java to Kotlin converter in action. We'll see that the converter doesn't always produce the best result. The converter will produce the Kotlin code that is very similar to its Java counterpart. Then we'll try to simplify it. So, let's start. The “updateWeather” Java function does a very simple thing: it assigns two different values to two variables depending on the temperature. The enum class ‘Color’ contains the colors used to describe the weather. We assign the description and the color values depending on the degrees. Probably, here it’s more a European way of perceiving the weather, for some people, less than 20 Celsius (or 70 Fahrenheit) would be already cold. It's a very straightforward code in Java, and we can convert it to Kotlin automatically. There are two main ways to use the converter from IDE. You can either convert the whole Java file to Kotlin, as we saw before, or you can copy the Java code and paste it into the Kotlin file. In this case, IDE will suggest converting it automatically. You can copy the Java code from any place, for instance, from a browser. Google the Java code, copy it, paste it in a Kotlin file, and see the analogous result. Here’s the ‘updateWeather’ function code converted to Kotlin. It looks very similar to its Java version. You see new ‘fun’ and ‘val’ keywords for function and variable declaration, but overall you understand what's going on. This function initializes two variables using a slightly different syntax. However, we can improve this code, make it more “Kotlinish”. What can be done? At first, we have to repeat the variable name several times. We say “description equals”, “description equals”, but in Kotlin we can avoid it. We can initialize two variables at once by a pair of values. The first value in each pair is used to initialize the first variable, ‘description’, the second value initializes the second variable ‘color’. Now we don’t have to repeat the ‘description’ variable name. The next improvement. When you define a variable in Java you have to specify its type. In Kotlin, you can prefer to omit the type if it's clear from the context. When you omit the type specification, the Kotlin compiler infers the type automatically, based on the initializer expression. IDE can do automatically the next simplification: we want to replace ‘if’ with ‘when’ expression. ‘when’ expression in Kotlin is similar to ‘switch’ in Java; it allows you to enumerate several options. It’s especially useful when you have more than two ‘if-else’ options: ‘when’ often looks nicer for this use case. We're almost done. We now have only one last change. To be more Kotlin-ish, we can replace the constructing of ‘Pair’ with ‘to’. That’s an alternative way to create a Pair in Kotlin. We'll discuss later in this module the details of what exactly it is and where ‘to’ comes from. That's the result. That’s how we converted the Java function into a Kotlin function. You can compare the initial code written in Java and the improved Kotlin version. The question to check yourself. Does the automatic Java to Kotlin converter always produce the idiomatic Kotlin code? The answers are ‘yes’ or ‘no’. Automatic Java to Kotlin converter is very useful. It’s very helpful in many situations, however, often it just converts the code *automatically*. To get the best results and to get idiomatic Kotlin code, you have to manually improve it: replace some constructs, rewrite some code, and so on. You've seen the examples of using Java to Kotlin converter. That already allows you to start experimenting with Kotlin if you want. Next, you'll learn the basics of the language.