liveBook · Manning

Interop

As explained in Kotlin in Action, 2E

Java interoperability is the ability of Kotlin code to interact directly with Java code and libraries, including exchanging compatible string values without conversion.

Kotlin interoperates directly with Java code and libraries on the JVM. It uses standard Java classes where that makes existing APIs familiar and reusable, while adding Kotlin syntax and library operations around them. IDE navigation can cross Kotlin and Java, and Java-to-Kotlin conversion can help bring existing code forward. section 1.6 section 3.1

Strings cross the Kotlin–Java boundary unchanged: a Kotlin String is the same string used by Java. Pass Kotlin-created strings to Java methods, and use Kotlin string operations on strings returned by Java, without conversions or wrapper objects. section 3.5

Use Java collections without conversion

Kotlin collection factories create standard Java collection classes, so a list or map can move between Kotlin and Java APIs directly. Kotlin presents collection interfaces as read-only by default, yet supplies operations such as last(), shuffled(), and sum() on the same underlying Java collections. section 3.1

button.setOnClickListener {     #1
    println("I was clicked!")
}

A Kotlin lambda can be passed directly where a Java API expects a single-abstract-method listener. Its parameter list corresponds to the interface method, avoiding the anonymous-class form required by older Java code. section 5.2

Extensions remain callable from Java. A top-level Kotlin extension compiles to a static method whose first argument is the receiver; the generated Java class name comes from the Kotlin file name. Java can statically import that method, although the Kotlin call is usually more readable. section 3.3.2

Constructor design may also follow Java API shapes. Secondary constructors are useful when a class needs multiple parameter lists or must initialize a Java superclass through different superclass constructors; each secondary-constructor path must reach a base-class constructor. section 4.2.2

Java nullability needs explicit care because Java does not encode it in its type system. Kotlin recognizes supported Java nullability annotations: @Nullable String becomes String?, and @NotNull String becomes String. Without annotations, Kotlin represents the Java type as a platform type. section 7.12

Interoperability is not limited to objects and collections. Kotlin lambdas work with Java functional interfaces such as Runnable, Callable, and listener interfaces, letting Kotlin call Java APIs with concise, idiomatic callback code. section 5.0 section 5.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains shared strings and collections, Java-callable extensions, SAM lambdas, constructors, and annotated nullability; defers collection type-system details.
Examples
Demonstrates Java collection implementations, a Java-callable lastChar extension, downloader constructors, and an OnClickListener lambda.
What it assumes
Assumes familiarity with Kotlin functions, classes, collections, and nullable types while introducing their Java boundary behavior.
What you can do after
Apply it via Java APIs by sharing strings and collections, passing SAM lambdas, calling extensions, and interpreting Java nullability annotations.
How it connects
Links Java interoperability with collections, strings, extension functions, secondary constructors, lambdas, functional interfaces, and nullability.

Also explained in

Compare how 2 books explain interop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.