Kotlin doesn't live in a vacuum: why and how to befriend other languages
\n\nImagine: 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\nThe 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\nIn 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\n1. Kotlin and Java: kindred spirits from the first lines
\n\nThe 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\nHow to call Java from Kotlin
\nSuppose 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\nIn 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\nEverything 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.
How to call Kotlin from Java
\nBackward 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\nFrom 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\nThe 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.
2. Kotlin and Python: two worlds, one JVM (and GraalVM)
\n\nNow 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\nOption A: GraalVM — running Python on the JVM
\nGraalVM 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\nInstall GraalVM and add Python support:
\n\ngu install python\n\n\nNow 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