View RSS Feed

Advanced Java

What is Java Generics?

Rate this Entry
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.

Java Code:
List list = new ArrayList();
list.add(new Integer(2));
These objects can also be casted that were added to list, as it is given below.
Java Code:
Integer integer = (Integer) list.get(0);
String  string   = (String) list.get(1);
Sometimes single type objects are being stored. Type of collection can be set in Java Generics.
Java Code: This is the code to
List<String> strings = new ArrayList<String>();
strings.add("a String");
String aString = strings.get(0);

Submit "What is Java Generics?" to Facebook Submit "What is Java Generics?" to Digg Submit "What is Java Generics?" to Submit "What is Java Generics?" to StumbleUpon Submit "What is Java Generics?" to Google