Most Java developers know the difference between int and Integer. But do you know why List<Integer> is not a subtype of List<Number>? Or that null has its own type — one you can never declare?
This fourth chapter in our Java SE 25 deep dive explores JLS Chapter 4 — the formal specification of Java's type system. We go beyond the surface to understand the rules that govern every variable, every cast, and every generic you write.
In this article, you'll explore:
- The precise distinction between primitive types (stored on the stack, direct value) and reference types (heap-allocated, pointer semantics).
- The full widening primitive conversion chain (
byte → short → int → long → float → double) and when a narrowing cast is required. - The mechanics of autoboxing and unboxing — including the JVM's integer cache (-128 to 127) and the
NullPointerExceptionrisk of unboxing. - Type erasure (JLS §4.6): why generic type information is removed at compile time and what reifiable types are.
- The special null type: why it's the subtype of all reference types, yet has no name.
This isn't a beginner's guide to generics. It's a formal, specification-grounded analysis of the type system that underpins every line of Java you write.
Read the full article: https://eldritch.codes/en/tome/tome-02-jvm-runtime/chapter-03
Part of the "Eldritch Codes" series — a comprehensive guide to Java SE 25, explored through the lens of the JLS and the original specs.
Top comments (0)