Calling Java from Kotlin

Calling code written in Java from within Kotlin .kt files and .kts scripts is straightforward for most part. Kotlin naturally integrates with Java, and we can use properties and methods without thinking twice—it almost always just works.

To see this in action, and learn ways to work around occasional glitches you may run into, let’s write a Java class that we will then use from Kotlin.

 package​ com.agiledeveloper;
 
 import​ ​java.util.List​;
 import​ ​static​ java.util.stream.Collectors.toList;
 
 public​ ​class​ JavaClass {
 public​ ​int​ ​getZero​() { ​return​ 0; }
 
 public​ List<String> ​convertToUpper​(List<String> names) {
 

Get Programming Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.