aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics doubt" Watch "Generics doubt" New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics doubt
 
Similar Threads
Doubt on Generics.
Generics
Generics
Generics
Generics