Posts

Java Generics

Image
  History Java supports generics as of version 1.5 Before list of integers List list of strings List list of lists of strings List After list of integers List<Integer> list of strings List<String> list of lists of strings List<List<String>> «Now compiler can track what we have list of»  Terms   Term Example Parameterized type List<String> Actual type parameter String Generic type List<E> Formal type parameter E Unbounded wildcard type List<?> Raw type List Bounded type parameter <E extends Number> Recursive type bound <T extends Comparable<T>> Bounded wildcard type List<? extends Number> Generic method static <E> List<E> asList (E[] a) Type token List.class Before-after generics // before generics List words = new ArrayList(); words.add( "Hello " ); words.add( "world!" ); String s = ((String)words.get(0))+((String)words.get(1)) assert s.equals( "Hello world!" );  // with gener