One of the design goals with Kotlin is interoperability with Java. Learn how to call Java code from Kotlin.
- [Narrator] Interoperability with Java is a major design goal for Kotlin. Many teams will have a large amount of Java code in their repositories. And there are far more libraries written in Java than in Kotlin, so being able to call Java code from Kotlin isn't just important, it is critical. Let's begin by creating a Java class. In the Project window, expand src, main, java, then right click on java. Choose New, Java Class.
We're going to name this class com.tekadept.demo.Animal with a capital A, and click Return. And we'll create our Java class. So we're going to say private, final String name. Private final String kind. This is going to say what kind of animal we're creating.
Private Integer weight, how much our animal weighs. Create our constructor. Public Animal String name, String kind, Integer weight. And we'll say this.name equals name. This.kind equals kind.
And this.weight equals weight. Then we'll start creating our getters. So we'll say public String getName. And it returns name. Public String getKind, which will return our kind.
Public Integer getWeight, and this is going to return our weight. And finally our lone setter, public void setWeight, which will take in an Integer for the weight. And we'll set this.weight equal to weight.
And also let's create a public method, and we'll call this public String. We're going to call this Show, and this is going to return the name. Basically you're just going to create a sentence here. Name is a, plus kind, and, say and weighs, then the weight.
And finally, pounds, and a semicolon. Okay so that's our animal. Now, let's go back to our main Kotlin file. And we'll create a function to handle the Java interop. And we'll call the function JavaInterop. Now this function doesn't take any parameters, and it's not going to return anything. It's just going to say val Frisky equals Animal.
And Frisky is the name. Frisky is a cat. And Frisky weight 10 pounds. And we'll do a print line, and we'll say Frisky.Show. Next we'll create a dog, and we'll call him Fido. Once again we'll create a new instance of the animal class. We'll say Fido is a dog, and weighs 30 pounds.
We'll do another print line, and show Fido. With Fido.Show. Finally, to change a property like weight we access it directly. Properties defined without setters, like name or kind, we can't change. Kotlin considers them to be val types, and will generate a compile time error. For instance we can change the weight of Frisky. We can say Frisky.weight equals 15 pounds, and we can do another print line Frisky.Show.
And that's fine. But, if we tried to do something like Frisky.kind. Tried to make Frisky say a puma, you notice this won't work. IntelliJ is recognizing that kind is a val type, it can't be reassigned, so let's just comment that out. Then let's come down here. And actually call the JavaInterop method. We'll compile our code, and run it.
And if we scroll back up to the top, we can see that Frisky is a cat, and weighs 10 pounds. Fido is a dog. And finally, Frisky is a cat again and now weighs 15 pounds. Overall, calling Java from Kotlin is easy and straightforward.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin