Java generics
Prior to Java 5, there are a lot of casting in collections, since in collection you add objects and cast the object you get back from collection. Provides compile-time type safety for collections and eliminates the pain of casting, without having ClassCastException. Generics creates strongly typed classes to do generic operation.
1 Vector v = new Vector(); 2 v.add("hello"); 3 v.add(new Integer(1)); 4 v.add(new HashTable()); 5 6 String s = (String)v.get(0);
- Performs strongly type check in compile time
- Eliminates casting
- Enable programmers to implement generic algorithms, which can be used on different types.
浙公网安备 33010602011771号