The BackendDeeper

Kayavak / The Backend / BE·33 Language Foundations / Deeper

Deeper · The Backend

Language Foundations

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

Java began in 1991 as "Oak", in Sun Microsystems' Green Project led by James Gosling, aimed at consumer devices. Those devices used many different processors, so the team compiled to bytecode for a virtual machine instead of to one chip. When the web arrived, the same property ("write once, run anywhere") made Java the language of browser applets; Java 1.0 shipped in January 1996. Applets faded, but the virtual machine, garbage collection and a large standard library made Java the main language for enterprise servers.

Its conservative design was deliberate: C-like syntax to be familiar, no pointer arithmetic or manual memory freeing to be safe, checked exceptions to make error handling visible. Sun open-sourced Java as OpenJDK (2006–07) and Oracle acquired Sun in 2010. Since 2018 Java has shipped every six months, and the language has gained records, pattern matching and sealed types that make it much more expressive than its 1990s self.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

Java code is compiled once to bytecode and turned into machine code by the JVM at run time. Every value is either a primitive (just bits) or a reference to an object on the heap; most surprises come from forgetting which.

Question Answer
What is it? A statically typed, class-based language compiled to JVM bytecode
Problem it solves Large, long-lived programs that run on any platform, with memory safety and strong tooling
Parts Types (8 primitives, references), classes and packages, exceptions, records, sealed types, generics, the standard library
How it works javac checks and compiles to .class files; the JVM loads, verifies, interprets and JIT-compiles them
Assumptions You know which comparisons are by value (primitives) and which by identity (references, unless equals)
When they change == on boxed Integers works for small values (cached) and fails for larger ones
Where it fails Start-up time and memory compared with native languages; verbosity (much reduced since Java 14–21)
Related ideas Bytecode and JIT (Foundations, ch10; ch40); types (Foundations, ch02)

What if?

What if Java had no primitive types, only objects?

Reason it through, then open

Every int would be a heap object with a header, so an array of a million numbers would be a million pointers to a million objects: several times the memory and far worse cache behaviour. Java kept primitives for speed, at the cost of two kinds of values and boxing between them. Project Valhalla's value classes aim to let objects without identity be stored as flatly as primitives.

What if Java exceptions were all unchecked?

Reason it through, then open

Signatures would no longer say which failures a method can produce, so callers would learn about I/O errors at run time. That's how most languages (C#, Kotlin, Python) work, and how most modern Java code behaves in practice, because lambdas and streams don't mix well with checked exceptions. The debate is about whether forcing callers to acknowledge errors helps more than the boilerplate it creates.

Where the boundary is

Aspect == equals()
On primitives Compares values Not applicable
On references Same object? Equal content, as the class defines it
Integer 127 vs 127 true (cached box) true
Integer 128 vs 128 Usually false true

How sure are we?

Claim How it's known Where from
Java 1.0 was released in January 1996 Historical Sun's release
Integer caches −128 to 127 Convention Required by the Java Language Specification for autoboxing; the upper bound can be raised
Checked exceptions improve reliability Active research Long debated; few controlled studies

Still open

Valhalla (value classes and better generics over primitives) is Java's largest ongoing change, and its final shape is still being refined in previews.

What would change your mind? If value classes made boxing invisible, the primitive/reference split would matter much less to everyday code.

Recall

Why does Java compile to bytecode?

To run unchanged on any processor with a JVM, which compiles it to native code at run time.

Why can `==` on two `Integer`s holding 128 be false?

They're different boxed objects; only −128 to 127 are cached and shared.

Why does Java keep primitive types?

They're stored directly as bits, avoiding the memory and cache cost of an object per value.

When did Java move to releases every six months?

2018, starting with Java 10.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.