Quick revision
The short answer
Java chose fixed-size primitives because in 1995 they gave C-like speed and a predictable memory layout, while objects gave uniformity at the cost of a header and a pointer per value. Generics, added later, work only over objects, which forced boxing and split the type system in two. Project Valhalla aims to close that gap with value classes that have no identity and can be stored flat; until it ships, a long-lived codebase should keep its value types immutable and free of identity-based behaviour, and should not put preview features into production.
What to remember
In this answer, we saw that Java split primitives from objects to get predictable speed and layout in 1995, and that generics over objects only made boxing unavoidable in collections and streams. Valhalla aims to let classes give up identity so the JVM can store them flat, with value classes first and better generics later, none of it final in Java 25. Today the right preparation is to stop relying on the identity of wrappers and value-based classes, to keep our own value types immutable and identity-free, and to keep preview features out of production without a plan.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java chose fixed-size primitives because in 1995 they gave C-like speed and a predictable memory layout, while objects gave uniformity at the cost of a header and a pointer per value. Generics, added later, work only over objects, which forced boxing and split the type system in two. Project Valhalla aims to close that gap with value classes that have no identity and can be stored flat; until it ships, a long-lived codebase should keep its value types immutable and free of identity-based behaviour, and should not put preview features into production.
Why this question is asked
This is an architecture question more than a syntax question. It asks why one of Java's oldest design decisions was made, what it costs us today, and how to keep a codebase that will live for ten more years ready for the change that is coming. Interviewers at senior levels ask it to see whether we can reason about language trade-offs and turn them into practical rules for a team. In this answer, we will look at why Java split primitives from objects, how generics deepened that split, what Valhalla proposes, and what we should do in our code today.
1Why Java Started With Two Kinds of Types
When Java was designed in the early 1990s, it had to run well on modest hardware and compete with C and C++. A primitive int is just 32 bits that the processor can add in one instruction, and an int[] is one block of memory. Those properties gave Java predictable performance for arithmetic, arrays and loops.
Objects gave something different: one uniform model in which every value has a class, methods, and an identity. Identity means each object is a distinct thing in memory, so two objects with the same contents are still two objects, and we can compare them with ==, synchronize on them, or mutate one without touching the other. Identity is what makes objects useful for things like accounts or connections, but it has a price. Every object needs a header, it is reached through a reference, and the JVM must keep it separate from every other object, which prevents it from being laid out flat inside an array or another object.
So Java offered both: primitives for raw speed and objects for modelling, with wrapper classes such as Integer to cross between them.
2How Generics Widened the Gap
Generics arrived in Java 5, ten years later, and they were designed to work over objects only. The type parameter in List<T> is erased to Object at runtime, so T can never be int. Every time we put an int into a collection or a stream, Java boxes it into an Integer, and the JDK had to add special primitive versions of many APIs, such as IntStream, IntFunction and ToIntFunction, to avoid that cost on hot paths.
Notice the last line. Even our own small types, such as a Point with two ints, have the same problem as Integer: an array of them is an array of references to scattered objects. Only the eight built-in primitives get a flat layout.
3What Valhalla Proposes
Project Valhalla is the long-running OpenJDK effort to let user-defined types behave like primitives where that matters. Its central idea is the value class: a class whose instances have no identity. A value object is defined only by its field values, so two value objects with the same fields are the same value. Without identity, the JVM is free to copy the object, keep it in registers, or store its fields directly inside an array or another object, which removes the header and the pointer.
The plan comes in stages. First come value classes and objects (the JEP for them has been in preview in early-access builds; check the current status for your JDK). Existing value-based classes in the JDK, such as Integer, Optional and LocalDate, are expected to become value classes. Later stages aim at null-restricted types and at generics that can work over values without boxing, which would finally let a List of numbers be stored flat. None of this is a final feature in Java 25.
| Aspect | Identity class today | Value class (Valhalla) |
|---|---|---|
| What == compares | the reference | the field values |
| synchronized on it | allowed | expected to fail |
| Header and pointer | always | can be removed by the JVM |
| Layout in an array | array of references | can be stored flat |
| Mutable fields | allowed | fields are final |
4What a Long-Lived Codebase Should Do Today
The good news is that preparing for Valhalla mostly means writing code that is good anyway. The first rule is to stop relying on the identity of value-like objects. We should not compare Integer, Long, Optional or LocalDate values with ==, not synchronize on them, and not use them as keys in an IdentityHashMap. The JDK already documents these classes as value-based for this reason, and recent JDKs warn about synchronizing on them.
The second rule is to design our own small domain types, such as Money, CustomerId or Coordinates, as immutable and identity-free. Records are a natural fit: final fields, equals and hashCode based on the contents, and no reason to compare them by reference. Such types should migrate to value classes with little more than a keyword when the feature is final.
The third rule is about preview features. A preview feature is complete but not final, and it can change between releases; class files compiled with --enable-preview only run on that exact Java release. So we can try value classes in a spike or a benchmark, but production code should wait for the final version, or keep preview use behind a clear plan to follow each release.
5Follow-up questions
Why not simply make int an object, as some languages do?
Languages that do this rely on the compiler and runtime to unbox values wherever possible, and they still pay for boxing in generic code. Java in 1995 could not count on such optimisations, and today it cannot change the meaning of existing code. Valhalla takes the other direction: rather than making int an object, it lets objects give up identity so they can be as cheap as int.
Will existing code that uses == on Integer break when Valhalla arrives?
Code that compares Integer values with == already gives wrong answers outside the cache range, so it is broken today. With value classes, == would compare values, which may make some of that code accidentally correct. Synchronizing on an Integer is the case expected to fail outright, which is why the JDK has warned about it since JDK 16.
How would you track Valhalla without adopting it early?
I would have one engineer follow the JEPs and early-access builds, run our key benchmarks against them once or twice a year, and keep a short list of the types that would benefit most, such as money and identifiers. The code rules about identity give us the readiness; the tracking tells us when to switch.
6Conclusion
In this answer, we saw that Java split primitives from objects to get predictable speed and layout in 1995, and that generics over objects only made boxing unavoidable in collections and streams. Valhalla aims to let classes give up identity so the JVM can store them flat, with value classes first and better generics later, none of it final in Java 25. Today the right preparation is to stop relying on the identity of wrappers and value-based classes, to keep our own value types immutable and identity-free, and to keep preview features out of production without a plan.
Open with
Java chose primitives because in 1995 it needed C-like performance: an int is 32 bits the processor adds in one instruction, and an int array is one contiguous block.
Then, if they want more
Objects gave a uniform model with identity, but every object costs a header and a pointer, and identity stops the JVM from laying objects out flat. Then generics came in Java 5, erased to Object, so they only work over references. That is why collections and streams box, and why the JDK grew IntStream and the other primitive specialisations.
Valhalla aims to resolve this by letting a class give up identity. A value object is defined only by its fields, so the JVM can copy it, scalarise it or store it flat in arrays. The plan is value classes first, with the JDK's value-based classes like Integer and Optional migrating, and later null-restricted types and generics over values.
It is not final in Java 25. For a codebase that will live a long time, I would do three things now. Ban identity-sensitive use of wrappers and value-based classes: no ==, no synchronizing on them. Model our own small domain types as immutable records with value equality, so they can migrate easily.
And keep preview features out of production unless we have a clear plan to track their changes release by release.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 16, Java 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Why did Java choose fixed-size primitives alongside objects instead of making everything...": https://scrivio-interview-library.netlify.app/java/types-and-values/3f0a8c90d2/