~/ learn/ java-from-zero/ cards/ Type erasure: generics at runtime
1 of 4

Type a runtime check that two generic lists share the same class

Type a runtime check that two generic lists share the same class

Answer

List<String> ss = new ArrayList<>(); List<Integer> ii = new ArrayList<>(); System.out.println(ss.getClass() == ii.getClass()); // true

Type arguments are erased from bytecode. Both ArrayList<String> and ArrayList<Integer> become ArrayList at runtime.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/e17f6566-5fd0-4dc0-a6b0-8b23459362fb/flashcard utf-8 LF