| Author |
Generics doubt
|
sentil kumar
Ranch Hand
Joined: Oct 23, 2006
Posts: 74
|
|
public class Test3 { public static void main(String[] args) { show(new Integer[]{1,23,12}); showObj(new Integer[]{1,23,12}); } static <E> void show(E[] inputArray) { for(E element : inputArray) { System.out.println(element); } } static void showObj(Object[] obj) { for(Object o : obj) { System.out.println(o); } } } in the above code, both show(),showObj() is giving the same result. what significance generics method adds to the code. Please explain the reason behind going for generics.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by sentil kumar: ...Please explain the reason behind going for generics.
Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked.
Ref: Sun - Generics.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Allan Jacobs
Greenhorn
Joined: Aug 28, 2006
Posts: 15
|
|
|
Generics are especially useful when using collections. When using collections, the use of generics serves as an assertion that all the members of a collection have the same or at least a compatible set of types.
|
 |
 |
|
|
subject: Generics doubt
|
|
|