~/ 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/6b691496-30e0-414d-84d2-d9356b283af9/flashcard utf-8 LF