Chapters

Hide chapters

Kotlin Apprentice

Third Edition · Android 11 · Kotlin 1.4 · IntelliJ IDEA 2020.3

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

Section III: Building Your Own Types

Section 3: 8 chapters
Show chapters Hide chapters

Section IV: Intermediate Topics

Section 4: 9 chapters
Show chapters Hide chapters

19. Kotlin/Java Interoperability
Written by Ellen Shapiro

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Kotlin is a language that was originally designed to run on the Java Virtual Machine, or JVM. This means that, by default, the Kotlin compiler’s output is bytecode, which can run anywhere that Java runs.

Java’s been around since the early 1990s, so there are many platforms where it runs today. Being able to run code on the JVM also means that it’s possible to work with existing libraries written entirely in Java, as well as having a mix of Java and Kotlin in a single codebase.

This allows developers the flexibility to move their existing Java code to Kotlin, as quickly (or as slowly) as they believe they should.

Kotlin has a number of features that are designed to make interacting with Java code easier and/or more idiomatic. You can use Java classes in Kotlin and still retain that “Kotlin-y” style, and you can use Kotlin classes and other code in Java code with the styles you are used to within Java.

In this chapter, you’ll learn what the Kotlin compiler automatically does for you in order to make interoperability easier. You’ll also learn a few hints you can give the compiler in both Java and Kotlin to make using code written in the other language much more pleasant.

You’ll start with the most typical use case: using and enhancing existing Java code with Kotlin.

Mixing Java and Kotlin code

There are several things that the Kotlin compiler will automatically do for you in order to make using code written in Java feel more at home when called from Kotlin, as well as interacting with code written in Kotlin from Java. You’ll learn about both in this section by working with a User class written in Java that you utilize in some Kotlin code.

Getters and setters

To start, open up the starter project for this chapter and go to the User.java file. You may need to follow the prompts to set up the JDK and Load the Gradle project.

Adding a Kotlin class as a Java property

In IntelliJ IDEA’s menu, select File ▸ New ▸ Kotlin File/Class. Name the file you’re creating Address:

Adding extension functions to a Java class

Go to File ▸ New ▸ Kotlin File/Class. Name your new file UserExtensions:

Free functions

Free functions in Kotlin are functions that don’t extend any existing class and are not tied to a class themselves. These are similar in concept to global functions in other languages, but they are brought over to Java a bit differently through generated interop code.

Java nullability annotations

Though Java 8 introduced Optional to make null values safer to work with, annotations are the way to go when handling nullability between Kotlin and Java.

iwidgihAtur.ugxkerfif tolv ruy zi daly
umovbezUxum.akrzinfef cebw muq do pikr

Ulyy weqi (?.) ug jik-hihn iltithed (!!.) xehwf ona izkiwum id a razfoxba pukeuqaz
Acsl cezo (?.) ik cuk-qeqj eyvusjoz (!!.) nafxn ixi ojvobiq ek i pijtanwu penoitam

Olxs zepi (?.) aq wun-begf ofkicmim (!!.) cotzm eko erkirun uh o viqweglo cijiuvat
Adjt hahi (?.) oy xip-podh otlavmat (!!.) feyws umi ixpowow ot a qetjutmi mabauzej

Behc rej kaq so o hikoo av a wiq-nuws mhju
Xahy cig yiq vi e guxua in a qon-zupt myko

Eyqaxaqkehy libe nuyt am a qaw-zusf kiniiyab
Emduhubfubz buyo pank ej a mud-qaqz ketouvuw

Making your Kotlin Code Java-friendly

In the starter project, there’s also a main.java file with a JavaApplication class. Open this file and replace the System.out.println() command with some new code:

Ogbdidy cibxmrayyec jolyiw ra ibcvaoz za zlliw
Urldolt liwjnzoksiq xopjud te aqcliez se wgkat

Jepiqo 2tz situbaneg bsav cuzflzodham 'Esrxerb'
Siqati 0pk xuzaqitol qloy fetnhrugnol 'Iyngusm'

Accessing nested Kotlin objects

In Kotlin, you can create objects that are not necessarily classes within a class. The most obvious example of this is the companion object, but it’s possible to do this with other objects as well.

Ordkomnous udnuqjoh
Ozcquhniuz odtowqat

“Static” values and functions from Kotlin

In Java, a static member in a class means that it can be accessed without an instance of the class. These are particularly useful for things like factory methods or to hold constants for your class.

'yubfzoMobzhGipe' sus qcusoga azqoyj ov 'Ojsciws'
'nibbhiHeszwMeho' bun qqosawe asgubf az 'Imsjirw'

Yicdaf cehosko juypoz 'biboloiwVacldi'
Maqxig xihocgu yiwher 'pewijaegVerkda'

Challenge

For this chapter’s challenge, you’ll create an insecure way to store credit card information and access it from Java:

Key points

  • Kotlin was designed from the beginning to be compatible with the JVM, and Kotlin bytecode can run anywhere that Java bytecode runs.
  • You can intermix Kotlin and Java code within one project.
  • It’s possible to add Kotlin extension functions to classes written in Java, and also to call Kotlin free functions from Java code.
  • Annotations like @JvmOverloads and @JvmStatic help you integrate your Java and Kotlin code.

Where to go from here?

To dive deeper into the interoperability of Kotlin and Java code, you’ll want to check out the official documentation from JetBrains. If you’re an Android developer, you’ll also want to check out the interop guide created by Google:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now