Kotlin Java Interop
Call Java from Kotlin and understand how Kotlin compares to Java. Platform types, nullability, SAM conversions, and more.
- Step 1
Comparison to Java
Kotlin fixes a series of issues that Java suffers from....
Start Lesson - Step 2
Calling Java from Kotlin
Kotlin is designed with Java interoperability in mind. Existing Java code can be called from Kotlin ...
Start Lesson - Step 3
Calling Kotlin from Java
Kotlin code can be easily called from Java. Instances of a Kotlin class can be created and used in J...
Start Lesson - Step 4
Using Java records in Kotlin
Records in Java are classes for storing immutable data with a fixed set of components. They have con...
Start Lesson - Step 5
Strings in Java and Kotlin
This guide shows how to do typical string tasks in Java and Kotlin, to help you migrate and write id...
Start Lesson - Step 6
Collections in Java and Kotlin
Collections are groups of a variable number of items that are significant to the problem and commonl...
Start Lesson - Step 7
Nullability in Java and Kotlin
Nullability is the ability of a variable to hold null. Dereferencing null causes NullPointerExceptio...
Start Lesson - Step 8
Standard input
Java Scanner is slow; use it only when you need its specific features (e.g. hasNext(), useDelimiter(...
Start Lesson - Step 9
The @Jvm annotations toolbox: when to use each
When you publish Kotlin code that Java teammates will consume, the @Jvm annotations control how your...
Start Lesson - Step 10
Platform types & nullability annotations
Every reference in Java can be null, but most Java code carries no nullability information. When Kot...
Start Lesson - Step 11
SAM conversions & fun interface
A SAM (Single Abstract Method) interface has exactly one abstract method โ Runnable, Callable, Compa...
Start Lesson - Step 12
Calling Kotlin from Java in depth
Java cannot see Kotlin the way Kotlin sees itself: there are no top-level functions, no companion ob...
Start Lesson - Step 13
@Throws & checked exceptions
Kotlin has no checked exceptions: every exception is unchecked, and the compiler never forces you to...
Start Lesson - Step 14
Generics interop: variance & wildcards
Java expresses variance at the use site with wildcards (? extends T, ? super T). Kotlin prefers decl...
Start Lesson - Step 15
Collections & arrays interop
Kotlin splits Java's single List into two views: a read-only List<T> and a MutableList<T>. They are ...
Start Lesson - Step 16
Property interop: getters, setters & accessor targets
Kotlin properties and Java accessor methods are two views of the same idea. A Java getX()/setX() pai...
Start Lesson - Step 17
vararg interop & the spread operator
A Kotlin vararg parameter and a Java varargs parameter (T...) compile to the same thing on the JVM: ...
Start Lesson - Step 18
Calling Java builders & fluent APIs from Kotlin
Many Java libraries use the builder pattern: a chain of methods that each return this (or a new buil...
Start Lesson - Step 19
Coroutines & suspend functions from Java
A Kotlin suspend function cannot be called directly from Java: under the hood the compiler adds a hi...
Start Lesson - Step 20
Scope functions for cleaner Java interop
Kotlin's scope functions โ let, run, with, apply, also โ shine when you work with Java objects that ...
Start Lesson - Step 21
Bridging Java functional interfaces & method references
Java 8 added java.util.function interfaces โ Function, BiFunction, Supplier, Consumer, Predicate, an...
Start Lesson - Step 22
Annotations, const & typealias across the boundary
Kotlin can declare and consume Java annotations. To create an annotation usable from both languages ...
Start Lesson - Step 23
Companion objects & @JvmStatic from Java
Kotlin has no `static` keyword. The role of Java statics is played by companion objects, top-level f...
Start Lesson - Step 24
Sealed classes, exhaustive when & Java switch
A `sealed` class or interface restricts its subtypes to a known, closed set declared in the same mod...
Start Lesson - Step 25
Inline value classes & @JvmInline
A value class wraps a single value to give it a distinct type โ a `UserId` that is not interchangeab...
Start Lesson - Step 26
Default arguments & @JvmOverloads
Kotlin functions can declare default values for parameters, so one declaration covers many call shap...
Start Lesson - Step 27
Extension functions seen from Java
An extension function lets you call `"abc".shout()` as if `shout` were a method on `String`, without...
Start Lesson - Step 28
Java Optional โ Kotlin nullable
Java 8 introduced `Optional<T>` to express 'a value that may be absent'. Kotlin solves the same prob...
Start Lesson