Kotlin as Glue: Integration with Java, Python, C and JS

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Kotlin doesn't live in a vacuum: why and how to befriend other languages

\n\n

Imagine: you're writing in Kotlin — a modern, concise, and safe language. But your project is not a blank slate. Somewhere in the system's depths lives legacy Java code, analysts need a Python library, and low-level operations require C. Or you want to reuse business logic on the frontend by writing it in Kotlin, but JavaScript rules there.

\n\n

The good news: integrating Kotlin with other languages is not a pain, but a superpower. Kotlin is designed to be a "glue language." It can run on the JVM (alongside Java), compile to native code (Kotlin/Native), and even to JavaScript (Kotlin/JS).

\n\n

In this article, we'll break down four key scenarios of Kotlin interaction with other languages using practical examples. You'll learn how to call Python from Kotlin, embed C code via JNI, seamlessly work with Java, and even use Kotlin on the frontend. Let's go!

\n\n

1. Kotlin and Java: kindred spirits from the first lines

\n\n

The most obvious and simple scenario is Kotlin Java interoperability. Since Kotlin compiles to JVM bytecode, it is 100% compatible with Java. You can call any Java code from Kotlin and vice versa — without bridges, adapters, or pain.

\n\n

How to call Java from Kotlin

\n

Suppose you have an old Java class for working with dates:

\n\n
// Java: DateUtils.java\npublic class DateUtils {\n    public static String formatDate(long timestamp) {\n        return new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date(timestamp));\n    }\n}\n
\n\n

In Kotlin you use it as if it were native:

\n\n
// Kotlin\nfun main() {\n    val formatted = DateUtils.formatDate(System.currentTimeMillis())\n    println("Today: $formatted") // Today: 2025-04-03\n}\n
\n\n

Everything works "out of the box." But there are nuances: null-safety. Java doesn't know about Kotlin's nullable types, so when calling Java methods, Kotlin treats return values as platform types (e.g., String!). Tip: always explicitly specify the type or use !! / ?. wisely.

\n\n

How to call Kotlin from Java

\n

Backward compatibility also exists. Suppose you have a Kotlin file:

\n\n
// Kotlin: Calculator.kt\npackage utils\n\nobject Calculator {\n    @JvmStatic\n    fun sum(a: Int, b: Int): Int = a + b\n}\n
\n\n

From Java we call it like this:

\n\n
// Java\nimport utils.Calculator;\n\npublic class Main {\n    public static void main(String[] args) {\n        int result = Calculator.sum(5, 3);\n        System.out.println(result); // 8\n    }\n}\n
\n\n

The main thing is not to forget the @JvmStatic annotation for static methods and @JvmName for resolving name conflicts. Integrating Kotlin with Java is so tight that you can mix both languages in one project — this is the standard for Android development and many Spring backends.

\n\n

2. Kotlin and Python: two worlds, one JVM (and GraalVM)

\n\n

Now this is more interesting. Kotlin Python is not the most trivial combination, but it is possible. There is no direct native bridge, but we can use JVM solutions.

\n\n

Option A: GraalVM — running Python on the JVM

\n

GraalVM is a virtual machine that can run multiple languages (Java, Kotlin, JavaScript, Python, Ruby) in a single process. This is an ideal tool for integrating Kotlin with Python.

\n\n

Install GraalVM and add Python support:

\n\n
gu install python\n
\n\n

Now in Kotlin code you can call Python functions:

\n\n
// Kotlin (GraalVM Polyglot API)\nimport org.graalvm.polyglot.*\n\nfun main() {\n    val context = Context.create("python")\n    // Exec                

Blogs

Book Recommendations