my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics doubt" Watch "Generics doubt" New topic

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) {

static void showObj(Object[] obj) {
for(Object o : obj) {
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

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
Allan Jacobs

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.
I agree. Here's the link:
subject: Generics doubt
It's not a secret anymore!