Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is type erasure? Give three things you cannot do because of it.

Type erasure means Java checks generic types at compile time and then removes most of them from the bytecode: List<String> becomes plain List, and a type parameter T becomes its bound, Object by default. Because the type arguments are gone at run time, we cannot write new T(), new T[] or T.class, we cannot test instanceof List<String> or overload f(List<String>) against f(List<Integer>), and we cannot use primitives such as List<int>. Casts that the compiler inserts at each use site keep the code type safe.

Why this question is asked

Every Java developer uses generics, but many are surprised the first time the compiler refuses new T() or tells them two methods have the same erasure. These errors only make sense once we know what happens to generic types after compilation. Interviewers ask this question because it shows whether we understand the model behind the syntax, which also explains reflection tricks used by frameworks like Jackson and Spring. In this answer, we will see what erasure does to our code, look at how the compiler keeps it type safe, and then go through the things erasure forbids, each with a short example and the usual workaround.

1A Quick Recap of Generics

Generics let us write a class or method once and use it with many types. A type parameter, such as T in class Box<T>, is a placeholder; a type argument, such as String in Box<String>, is what we fill it with when we use the class. The compiler then checks that we only put strings into a Box<String> and lets us take them out without a cast.

Generics were added in Java 5, about ten years after Java 1.0. By then there were millions of lines of code using collections without type arguments, and the designers wanted that old code and the new code to work together without changing the JVM. Erasure is how they did it: the type checking happens entirely in javac, and the bytecode looks almost the same as before.

2What Erasure Does to Our Code

The compiler takes our generic code, checks every use of the type parameters, and then rewrites it. Each type argument is removed, and each type parameter is replaced by its bound. With no bound, the bound is Object. With <T extends Comparable<T>>, the erased type is Comparable.

Java
// What we write
class Box<T> {
private T value;
void set(T v) { value = v; }
T get() { return value; }
}
Box<String> b = new Box<>();
b.set("hi");
String s = b.get();
// Roughly what the bytecode says after erasure
class Box {
private Object value;
void set(Object v) { value = v; }
Object get() { return value; }
}
Box b = new Box();
b.set("hi");
String s = (String) b.get(); // cast inserted by the compiler
Step throughFrom source to bytecode
  1. We write Box<String> and Ttype arguments in the source
  2. javac checks every useonly strings go in, strings come out
  3. javac erases the typesBox<String> -> Box, T -> Object
  4. javac inserts casts(String) b.get() at each use site
  5. JVM runs one Box classno record of String at run time

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

Notice the cast on the last line. We never wrote it, but the compiler adds it at every place where a generic value is used as a specific type. This is what keeps erased code type safe: the compiler has already proved that only strings go into this box, so the inserted cast can never fail unless someone bypassed the checks with raw types or unchecked casts.

So at run time there is only one Box class, and a Box<String> and a Box<Integer> are the same kind of object. We can see it directly: new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass() is true.

3You Cannot Create a T: new T(), new T[] and T.class

The first thing erasure takes away is creating objects of the type parameter. At run time the method does not know what T is, so it has no class to call a constructor on, no element type for an array, and no Class object to hand out.

Java
class Factory<T> {
T make() { return new T(); } // error: unexpected type
T[] makeArray() { return new T[10]; } // error: generic array creation
Class<T> type() { return T.class; } // error: cannot select from a type variable
}
// Workaround: let the caller pass the type information in
class Factory<T> {
private final Supplier<T> maker;
Factory(Supplier<T> maker) { this.maker = maker; }
T make() { return maker.get(); }
}
Factory<StringBuilder> f = new Factory<>(StringBuilder::new);
f.make(); // a new StringBuilder

The fix is always the same idea: the caller knows the real type, so the caller passes something that carries it, such as a Supplier<T>, a Class<T> or an IntFunction<T[]> for arrays.

4You Cannot Test or Overload on Type Arguments

The second thing erasure takes away is any run-time check that depends on a type argument. An object only knows it is an ArrayList, not an ArrayList of what, so instanceof List<String> has nothing to test. The compiler rejects it; we can only test List<?> or the raw List.

The same reason forbids overloads that differ only in type arguments. After erasure both methods below have the signature f(List), and a class cannot have two methods with the same signature.

Java
Object o = List.of("a");
if (o instanceof List<String> l) { } // error: Object cannot be safely cast to List<String>
if (o instanceof List<?> l) { } // fine: true
void f(List<String> names) { }
void f(List<Integer> ids) { } // error: name clash, both methods have the same erasure

To work around the overload problem, we give the methods different names, such as findByNames and findByIds, which is usually clearer anyway. Note one subtle exception: since Java 16 pattern matching, instanceof List<String> l is allowed when the static type already guarantees it, such as testing a Collection<String>, because then no run-time check of String is needed.

5You Cannot Use Primitives as Type Arguments

The third limit is that type arguments must be reference types. After erasure, T becomes Object, and an int is not an Object, so List<int> cannot work. We write List<Integer> instead, and Java boxes each int into an Integer object.

Java
List<int> a = new ArrayList<>(); // error: unexpected type, required reference
List<Integer> b = new ArrayList<>();
b.add(42); // autoboxed to Integer.valueOf(42)
int x = b.get(0); // unboxed: 42

Boxing costs memory and time: each Integer is a separate object with a header, so a list of a million ints takes several times more memory than an int[] of the same length. This is why the JDK has IntStream and why Project Valhalla is working on value classes and specialised generics.

6Follow-up questions

Is all generic information really gone at run time?

No. Erasure removes type arguments from objects and from the code that runs, but the class file keeps the declared generic signatures of fields, methods and superclasses. Reflection can read them with Field.getGenericType and similar methods, which is how Jackson knows a field is List<Order>. What is truly gone is the type argument of an individual object, such as a particular ArrayList instance.

Why does the compiler warn about unchecked casts?

A cast like (T) obj or (List<String>) obj cannot be checked at run time, because the target type is erased. The compiler warns us that type safety now depends on our own reasoning. If we are wrong, the failure appears later, at a compiler-inserted cast far from the real mistake.

Can we write a static field of type T?

No. There is one class shared by all parameterisations, so a static field cannot depend on a type argument that differs between Box<String> and Box<Integer>. Static members can use their own generic methods instead.

7Conclusion

In this answer, we saw that type erasure checks generic types at compile time and then replaces type arguments with nothing and type parameters with their bounds, Object by default, while casts inserted at each use site keep the code safe. Because the type is gone at run time, we cannot write new T(), new T[] or T.class, cannot test instanceof List<String> or overload on type arguments, and cannot use primitive type arguments. In each case the workaround is to pass the type information in or to use a wrapper type.

What to say in the interviewAbout 80 seconds aloud

Open with

Type erasure means generic types exist for the compiler but mostly not for the JVM. javac checks that I only put strings into a List<String>, then erases the type argument, so in bytecode it is just List, and a type parameter T becomes its bound, which is Object unless I declared something like T extends Comparable.

Then, if they want more

To keep things safe, the compiler inserts casts at each use site, so String s = list.get(0) really becomes a cast from Object to String. Java chose this in Java 5 so old non-generic code and new generic code could work together without a JVM change.

Three things I cannot do because of it: first, create from T, so no new T(), no new T[10], no T.class, because at run time the method does not know what T is; the fix is to pass a Supplier, a Class or an array generator.

Second, check type arguments at run time, so no instanceof List<String> and no overloading f(List<String>) against f(List<Integer>), since both erase to f(List). Third, use primitives as type arguments, so List<int> is illegal and I use List<Integer>, which boxes every value.

About this answer

Last updated
12 November 2026
Versions it names
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.
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 "What is type erasure? Give three things you cannot do because of it.": https://scrivio-interview-library.netlify.app/java/generics/c33bb322da/

More in Generics