CKTIS / Java
Java type system
Java type system: Java has a static, nominal type system that distinguishes primitive from reference types and derives subtyping from declared inheritance and interface implementation.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
type systemJava has a static, nominal type system that distinguishes primitive from reference types and derives subtyping from declared inheritance and interface implementation.
Model closed variants with sealed types and pattern matching, and use generics to check collection element types at compile time.
List is not a subtype of List, arrays are covariant and can throw ArrayStoreException, and erasure removes most type arguments at run time.
null belongs to every reference type, inference can produce an intersection type, and a raw type bypasses checks and can cause heap pollution.
The sealed switch returns circle:2, generic lists share a run-time class so erased=true, and the covariant array ends with array-store=true.
Examples
import java.util.*;
public class Main {
sealed interface Shape permits Circle,Square {}
record Circle(int radius) implements Shape {}
record Square(int side) implements Shape {}
static String kind(Shape s){ return switch(s){case Circle c->"circle:"+c.radius();case Square q->"square:"+q.side();}; }
public static void main(String[] args) {
System.out.println(kind(new Circle(2)));
System.out.println("erased="+(List.of("x").getClass()==List.of(1).getClass()));
Number[] values=new Integer[1];
try { values[0]=1.5; } catch(ArrayStoreException ex) { System.out.println("array-store=true"); }
}
}
Result
circle:2 erased=true array-store=true
The sealed switch returns circle:2, generic lists share a run-time class so erased=true, and the covariant array ends with array-store=true.