Java instanceof with generic types
Codemia Knowledge Hub ·
You cannot write instanceof List<String> in Java, because generic type arguments are erased and are not available at runtime. Check against the wildcard form List<?> instead, and if the element type matters, inspect the elements yourself.
Generics mostly help at compile time, while instanceof checks the runtime type of an object you already have. Type erasure is what separates the two.
What instanceof checks
instanceof answers a runtime question: is this object an instance of this class or interface?
If value is null, the result is false. If the object implements an interface or extends a class being tested, the result is true.
Modern Java also allows pattern matching with instanceof, which combines the check and the cast:
What generics give you
Generics improve compile-time type safety:
Because the list is declared as List<String>, the compiler prevents you from adding unrelated types and removes the need for casts when reading.
Why instanceof List<String> does not compile
This fails:
At runtime, Java generally knows that the object is some kind of List, but not whether it is a List<String> or a List<Integer>. After compilation, both are effectively just List for runtime type checking.
Checking List<?> instead
You can check against the raw type, or better, against a wildcard:
This is legal because List<?> means "a list of some unknown type," which needs no runtime knowledge of the erased type argument. It does not tell you the object is a List<String>, so you cannot conclude the elements are String values.
Avoid using raw List everywhere just to get instanceof to compile, since that throws away compile-time safety. Prefer List<?> when you mean "a list of unknown element type."
Validating element types at runtime
If the real question is "is this a list whose elements are strings," you have to inspect the contents:
This checks the elements, not generic metadata, which is the only honest runtime check available.
Wildcards in method signatures
Often a generic signature removes the need for a runtime check altogether:
This method accepts List<Integer>, List<Double>, and other numeric lists because it reads values as Number. More generally, a runtime type check is sometimes correct, but polymorphism, interfaces or generic method signatures are usually the better design.
Related reading
- Java Multiple Inheritance
- Java Singleton and Synchronization
- Jersey stopped working with InjectionManagerFactory not found
- Limitation with classes derived from generic classes in swift
- Java int to String - Integer.toString(i) vs new Integer(i).toString()
- Java Integer equals vs.
- make arrayList.toArray return more specific types
- Making certain methods visible only to particular packages
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseObject-Oriented Design practice on Codemia
Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.