~/ learn/ java-from-zero/ cards/ Why generics: type safety and the diamond operator
1 of 4

Type a List<String> with the diamond operator

Type a List<String> with the diamond operator

Answer

List<String> names = new ArrayList<>();

The diamond <> tells the compiler to infer the type argument from the left-hand side declaration. No cast needed when you call names.get(i).

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