Java interoperability
A Scala class is a JVM class. It compiles to the same bytecode a Java compiler writes, it extends and implements Java types, and it is called by Java code with no wrapper and no bridge. The two directions are worth seeing once, because the details a reader meets are about names and class paths, not about the languages: an object becomes a class with a $ in its name plus a static forwarder, and an @main method becomes a class with a static main.
Scala calls Java
The JDK is the largest Java library there is, and a Scala program calls it without an import of anything but the class. LocalDate is a Java class with Java methods; the argument DayOfWeek.THURSDAY compares with ==, which in Scala is equals, and the month name comes from the Java formatting API:
dates.scala
import java.time.format.TextStyle
import java.time.{DayOfWeek, LocalDate}
import java.util.Locale
@main def dates(): Unit =
val date = LocalDate.of(2026, 11, 26)
println(date.getDayOfWeek == DayOfWeek.THURSDAY)
println(date.getMonth.getDisplayName(TextStyle.FULL, Locale.UK))
scala-cli run --server=false dates.scala
true
November
A Java class of your own is the same call. javac compiles it into a folder, scalac -cp reads that folder, and the Scala compiler checks the call against the Java signature exactly as it checks a Scala one — the parameters, the return type and the static keyword. The class path is the only extra argument the compiler needs.
Java calls Scala
The other direction has one rule: a Scala object is a singleton, and the compiler writes a class Doubler whose static methods forward to the singleton. Java therefore calls Doubler.twice(21) like a static method, and javac needs the Scala library on its class path to read the class:
java_calls_scala/Doubler.scala
object Doubler:
def twice(n: Int): Int = n * 2
java_calls_scala/UseDoubler.java
public class UseDoubler {
public static void main(String[] args) {
System.out.println("twice 21 is " + Doubler.twice(21));
}
}
cd java_calls_scala
mkdir -p out
scalac -color:never -d out Doubler.scala
javac -cp out:/opt/scala/lib/scala3-library_3-3.10.1.jar -d out UseDoubler.java
java -cp out:/opt/scala/lib/* UseDoubler
twice 21 is 42
javap shows the forwarder the compiler wrote: a final class with the static method, beside the Doubler$ class that holds the instance. That pair is how every Scala object looks from Java, and it is also why a Java program never writes new for it:
javap -cp out Doubler
Compiled from "Doubler.scala"
public final class Doubler {
public static int twice(int);
}
An @main method gets the same treatment: the compiler writes a class named after the method with a static main(String[]), so Java starts a Scala program with java -cp ... hello, and a Scala program can start a Java one with ProcessBuilder exactly as it starts anything else.
Related
- scalac, java -cp and Java side by side: the compiler and the class path this lesson builds on.
- What Scala is: the JVM both languages target.
- Packaging: a jar that a Java program can add to its class path.
- Java: the language on the other side of the call.
Article author: Arthur Isaev