Java, Java Generic, Programming, Software Development, Coding.
but it’s not type-safe. If we accidentally added an object that wasn’t a String, the code would throw a ClassCastException at runtime. Now, let’s use a generic List: List<String> list = new ArrayList<>(); list.add("hello"); String s = list.get(0); // No cast needed If you try to add a non-String