What is Java Generics?
by, 02-18-2012 at 04:32 PM (790 Views)
Java provides support to the Generic features of JV. For specification of the concrete types to certain general methods or classes, Generics is used.
Above given definition is an abstract definition. For better understanding, consider few examples.
Object instances are stored by the help of List interface. Therefore one can do storage and also place objects in a List. E.g.
These objects can also be casted that were added to list, as it is given below.Java Code:List list = new ArrayList(); list.add(new Integer(2)); list.add("String");
Sometimes single type objects are being stored. Type of collection can be set in Java Generics.Java Code:Integer integer = (Integer) list.get(0); String string = (String) list.get(1);
Java Code: This is the code toList<String> strings = new ArrayList<String>(); strings.add("a String"); String aString = strings.get(0);