Java interop
Programs compiled for the JVM can use any Java class: the whole JDK, and any library on the classpath. Zyn classes can extend Java classes and implement Java interfaces, so Zyn can be used where Java code expects your classes: frameworks, servers, game plugins.
#Using Java classes
Java classes are imported like Zyn files. When there's no .zyn file at that path, import looks for a Java class:
import java.util.{ArrayList, HashMap, Random}
import java.lang.{Math, StringBuilder}
let random = Random(42)
println(random.nextInt(100) == Random(42).nextInt(100))
println(Math.max(3, 7))
let sb = StringBuilder()
sb.append("Hello").append(", ").append("Java")
println(sb.toString())
let names = ArrayList<String>()
names.add("Ada")
names.add("Grace")
println(names.size())
let counts = HashMap<String, Int>()
counts.put("apples", 3)
println(counts.get("apples"))true 7 Hello, Java 2 3
Constructors are called like functions (no new), and static members through the class: Math.max(3, 7), Integer.MAX_VALUE. Everything is type-checked like Zyn code.
Programs that use Java need the JVM: zyn run compiles them first, by itself. They can't be native, and the REPL can't use Java classes yet.
#Libraries
The easy way to use a library is a project: name it in build.zyn, and Zyn downloads it (and what it needs) from Maven Central or any Maven repository, and puts it in your jar:
dependencies {
implementation("com.google.code.gson:gson:2.11.0")
}For a single file, --classpath adds jars, or folders of classes, for run, test and build:
zyn run --classpath libs/gson.jar app.zyn
zyn build --classpath "libs/*" app.zyn # every jar in libs
zyn build --classpath "server/libraries/**" plugin.zyn # also in subfoldersSeparate entries with : (; on Windows). Libraries given with --classpath aren't copied into the jar.
#Types
| Java | Zyn |
|---|---|
int, long, short, byte (and Integer, ...) | Int |
double, float | Float |
boolean | Bool |
char | Char |
String | String |
Object | Any |
java.util.List<T>, java.util.Map<K, V> | MutableList<T>, MutableMap<K, V> (Zyn's read-only ones can be passed too) |
| Arrays | List (copied in and out) |
void | Unit |
Zyn's Int is 64 bits. Passing one that's too big to an int parameter stops the program with an error, instead of silently cutting it.
#Getters as properties
As in Kotlin, Java getters can be read like properties: user.name calls getName(), and player.isOp calls isOp().
import java.time.LocalDate
let date = LocalDate.of(2026, 3, 14)
println(date.dayOfWeek)
println(date.year)
println(date.isLeapYear)SATURDAY 2026 false
#Nullability
Java doesn't say which results can be null, but many libraries mark them with a @Nullable annotation. Zyn reads those (also JetBrains' and others kept only in the class file), so their results have a ? type and must be checked:
- Results annotated
@Nullableare nullable:String?. - Other results are taken to be non-null, and checked when they arrive: a
nullstops the program with an error that names the method, instead of causing trouble later. - Where
nullis fine anyway, say so:let value: Int? = map.get(key)accepts anull.
import java.util.HashMap
let m = HashMap<String, Int>()
m.put("x", 1)
let found: Int? = m.get("x")
let missing: Int? = m.get("nope")
println("{found} {missing}")1 null
#Lambdas for Java interfaces
A lambda can be passed where Java expects an interface with one method (Runnable, Comparator, Consumer, an event listener, ...):
import java.lang.Thread
import java.util.ArrayList
let t = Thread { println("hello from a thread") }
t.start()
t.join()
let words = ArrayList<String>()
words.add("pear")
words.add("fig")
words.add("banana")
words.sort { a, b -> a.length - b.length }
println(words)
println(words.stream().map { it.uppercase() }.toList())hello from a thread [fig, pear, banana] [FIG, PEAR, BANANA]
#Extending Java classes
Zyn classes can extend Java classes and implement Java interfaces, overriding their methods with override:
import java.lang.Thread
import java.util.{ArrayList, Comparator}
class Worker(let job: String) : Thread() {
override fn run() {
println("working on {job}")
}
}
class ByLength : Comparator<String> {
override fn compare(a: String, b: String): Int = a.length - b.length
}
let w = Worker("the docs")
w.start()
w.join()
let words = ArrayList<String>()
words.add("ccc")
words.add("a")
words.add("bb")
words.sort(ByLength())
println(words)working on the docs [a, bb, ccc]
The type checker makes sure abstract methods are implemented and overrides match Java's parameters. Where Java's signature differs from Zyn's (an int where Zyn has Int), the compiler adds the bridge Java needs.
#Annotations
Classes, methods, functions and properties can have Java annotations, with constant arguments: numbers, strings, booleans, Java enum constants and lists of them.
import java.lang.Deprecated
@Deprecated(since = "2.0", forRemoval = true)
fn oldApi(): Int = 1They're real annotations in the class files, so frameworks that look for them find them, like an event handler in a server plugin:
import org.bukkit.event.{EventHandler, Listener}
import org.bukkit.event.player.PlayerJoinEvent
class JoinListener : Listener {
@EventHandler
fn onJoin(event: PlayerJoinEvent) {
event.player.sendMessage("Welcome, {event.player.name}!")
}
}#Java using Zyn classes
A Zyn class is a JVM class with its own name. In a project, it's in the project's package: class JoinListener in the project hello-zyn is zynproject.hello_zyn.JoinListener, and that's the name to give tools that load classes by name, like a plugin's plugin.yml. Java can make objects of it directly, before the Zyn program's main runs: the file's top-level values are set up the first time any of its classes is used.
The repository has a complete example: a Minecraft (Paper) plugin written in Zyn, in examples/minecraft-plugin. It's a project that compiles against Paper's API, with its plugin.yml in a resources folder.