Scrivio Interview library
ConceptPrincipal7 min read · updated 12 November 2026

Java chose erasure for generics in Java 5 while C# chose reified generics. Explain why, what it has cost the platform over two decades, and how Project Valhalla is addressing parts of it.

Java chose erasure in Java 5 for migration compatibility: existing code and libraries using raw collections kept working beside new generic code, and no JVM change was needed. C# took the opposite path with reified generics, which required a new runtime version and a new set of collection libraries. Erasure has since cost Java boxing for primitives, no run-time checks on generic types, heap pollution and awkward generic arrays, and Project Valhalla is addressing the boxing part with value classes and, later, generic specialisation; the JEPs are still in progress, so check their current status.

Background to this question

This question is less about syntax and more about platform judgment. A senior engineer is expected to explain why a reasonable team made a decision that later looked costly, what that decision has really cost, and how the platform is paying it down. The same reasoning applies to our own systems whenever we choose between a clean break and a compatible migration. In this answer, we will recap the two models, look at the situation in 2004 that drove Java's choice, contrast it with what C# did, go through the costs one by one, and then see how Valhalla approaches them and what lesson we take away.

1Two Ways to Implement Generics

With erasure, the compiler checks generic types and then removes the type arguments. At run time there is one ArrayList class, and an ArrayList<String> and an ArrayList<Integer> are the same kind of object. The compiler inserts casts where values come out.

With reified generics, the type arguments are real at run time. The runtime knows that a List<int> holds ints, can check casts to List<string>, and can create specialised code for each value type argument, so a List<int> stores raw ints without wrapping each one in an object. C# and the .NET runtime have worked this way since version 2.0 in 2005.

Erasure in Java and reification in C#
AspectJava (erasure)C# (reified)
Type arguments at run timeGone from objectsKept and checkable
Primitive or value argumentsNot allowed, use boxesAllowed, specialised code
typeof(T), new T()Not possiblePossible (with constraints)
Old non-generic codeWorks unchanged with generic codeSeparate old and new collections
Runtime change neededNoneNew CLR version

2Why Java Chose Erasure in Java 5

In 2004 Java was nine years old and enormous. Every enterprise codebase, every application server and every library used java.util collections as raw types, a Vector holding Objects. The designers set a hard requirement often called migration compatibility: a library could add generics without forcing its callers to change, and a caller could adopt generics while still using libraries that had not.

Erasure meets that requirement neatly. Because List<String> compiles to the same bytecode as List, a generified library is binary compatible with old callers, and raw types let old code call new code. The JVM did not need to change at all, which mattered when there were many JVM implementations from different vendors. The design was built on years of research, notably the GJ (Generic Java) work, which had shown it could be done this way.

3What C# Did Differently

Microsoft was in a different position. .NET was only a few years old with a much smaller installed base, and Microsoft controlled the single runtime. So C# 2.0 changed the runtime to understand generics, and added a new namespace, System.Collections.Generic, beside the old System.Collections. Old code kept using ArrayList and Hashtable; new code used List<T> and Dictionary<TKey, TValue>, and the two did not mix directly.

That was a cleaner design, but it had a price: two collection libraries for years, adapter code between them, and a runtime change that every deployment had to adopt. For Java, with many vendors and a much larger body of code, the same move would have split the ecosystem.

4What Erasure Has Cost Over Two Decades

The biggest cost is boxing. Type arguments must be references, so a List<Integer> holds a pointer to a separate Integer object for every element. A list of 10 million ints therefore takes roughly 10 million object headers plus pointers, several times the 40 MB that an int[] would need, and reading it means chasing pointers across memory, which defeats CPU caches. The JDK grew workarounds such as IntStream, LongStream and many primitive-specific functional interfaces, and libraries such as fastutil and Eclipse Collections exist largely to fill this gap.

The second cost is that the runtime cannot check generic types. A cast to List<String> is unchecked, instanceof List<String> is not allowed, and a method cannot ask what T is. This leads to heap pollution, a variable of type List<String> that actually refers to a list holding an Integer, which fails later with a ClassCastException far from the real mistake. The third cost is awkward arrays: arrays check their element type at run time, so new T[] and new List<String>[10] are forbidden, and varargs of generic types produce warnings and need @SafeVarargs.

Java
List raw = new ArrayList<String>();
raw.add(42); // compiles with an unchecked warning
List<String> names = raw; // heap pollution: List<String> holding an Integer
String first = names.get(0); // ClassCastException: Integer cannot be cast to String
List<Integer> boxed = new ArrayList<>();
for (int i = 0; i < 10_000_000; i++) boxed.add(i); // 10 million Integer objects
int[] flat = new int[10_000_000]; // one object, 40 MB of data

Notice that the error appears at the read, not at the add that caused it. That distance between cause and failure is the everyday face of erasure's cost.

5How Project Valhalla Approaches It

Valhalla is the OpenJDK project working on these costs, and it has taken a long time precisely because it must stay compatible with the code erasure protected. Its first major piece is value classes: classes whose instances have no identity, so the JVM is free to store them flattened, as their fields laid out directly in an array or another object, instead of as separate heap objects with headers. The JEP for value classes and objects has been available as a preview in early-access builds; check the current JEP status for your JDK version before relying on any of it.

The later piece is generic specialisation, often discussed together with null-restricted types, so that a List of a value class, or eventually of int, can store values directly instead of boxes. The plan is to do this without abandoning erasure for existing code, so that today's generic libraries keep working and can be specialised later. It is the same principle as in 2004: move forward without breaking the ecosystem.

Step throughJava generics over time
  1. Java 5 (2004)generics by erasure, migration compatible
  2. Java 8 (2014)lambdas plus primitive streams to dodge boxing
  3. Valhalla researchvalue types explored since about 2014
  4. Value classes previewidentity-free objects, flattened storage
  5. Generic specialisationlater step, check current JEP status

5 steps in order. Play them, or step through them one at a time.

6The Lesson for Platform Decisions

Erasure is often called a mistake, but it is better described as a trade: Java bought a smooth migration for millions of developers and paid for it with costs that grew as performance expectations rose. At platform scale, migration compatibility is a very strong force, because the cost of a split ecosystem is paid by everyone at once, while the cost of a compromise is paid gradually. When we design a public API or a data format in our own organization, we face the same choice, and the honest answer is usually to keep compatibility while leaving a path, as Valhalla does, for a better model later.

7Follow-up questions

Could Java have reified generics in Java 5 without breaking code?

Not without a large cost. It would have needed JVM changes from every vendor and either new collection classes, as C# added, or complex bridging between raw and reified versions of the same class. The designers judged that a split library would slow adoption, and given how quickly generics spread through the Java ecosystem, that judgment held up.

Will Valhalla make erasure go away?

Not for existing code. The aim is to let value types and, later, specialised generics coexist with erased generics, so a List<String> keeps behaving as it does today. Boxing costs can shrink for value types, but the general inability to ask what T is at run time is expected to remain for reference types.

What can we do today about boxing costs?

Measure first. Where profiling shows boxing on a hot path, use primitive arrays, primitive streams, or a primitive collection library. Elsewhere, keep ordinary generic collections, which are clearer and fast enough for most code.

8Conclusion

In this answer, we saw that Java chose erasure for migration compatibility: old raw-type code and new generic code worked together and no JVM change was required, while C# added reified generics with a new runtime and new collection libraries. Over two decades erasure has cost Java boxing of primitive type arguments, no run-time checks on generic types, heap pollution and awkward generic arrays. Project Valhalla is addressing the boxing part with value classes and later generic specialisation, still in progress, and the broad lesson is that compatibility is a powerful force in platform design.

What to say in the interviewAbout 80 seconds aloud

Open with

Java chose erasure because of migration compatibility. In 2004 there was a huge body of code using raw collections, and the requirement was that a library could add generics without breaking its callers and that no JVM change would be needed across all the vendors.

Then, if they want more

Erasure does that: List<String> compiles to the same bytecode as List. C# was in a different position, a young platform with one runtime, so it reified generics in the CLR and shipped a new System.Collections.Generic library next to the old one. The costs for Java over twenty years are real.

Type arguments must be references, so every int in a List<Integer> is boxed, which costs memory and cache locality, and we ended up with IntStream and primitive collection libraries. The runtime cannot check generic types, so we get unchecked casts and heap pollution that fails far from the cause. Generic arrays are forbidden and generic varargs need @SafeVarargs.

Valhalla is addressing the boxing with value classes, identity-free objects the JVM can flatten, and later with generic specialisation; those are still preview or planned, so I would check the current JEP status. The lesson I take is that at platform scale, compatibility usually wins, and the right move is to leave a path to improve later.

About this answer

Last updated
12 November 2026
Versions it names
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.
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 "Java chose erasure for generics in Java 5 while C# chose reified generics. Explain why...": https://scrivio-interview-library.netlify.app/java/generics/4854555f0d/