Generics
Generics provide type safety to to the Java programming. Generics were first introduced in Java5.0. Before Generics a typical list handling program in Java used to be://Make a new list List nameList = new ArrayList(); //Now add a string object to the list. Note that we can put any type of object in the list nameList.add(new String("bubba"); //While fetching we have to know beforehand that the object returned is of type String otherwise //it will throw an exception String name = (String)nameList.iterator().next();
With generics the change in the code is as follows:
//Make a new list. Notice the change List<String> nameList = new ArrayList<String>(); //Now add a string object to the list. Now we can put only objects of type String or its subtype nameList.add(new String("bubba"); //Now we can safely fetch the object String name = nameList.iterator().next();
Those coming from C++ background will find the generics resemblance to Templates. However there is one major difference. In C++ the templates types leads to generation of code for different types but the Java generics do not generate code for different types but handles it at runtime.
Back To Java Home
Back To Home
Sidebar
Last wiki comments
- Introduction to ORM: ugg boots
- Introduction to ORM: ugg boots
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
Sidebar
Random Pages
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Prospective MLA for Pune election - 2009: ugg boots
Wed 01 of Sep., 2010 12:58 IST
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
Post new comment