This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Good Reference on Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Good Reference on Generics" Watch "Good Reference on Generics" New topic
Author

Good Reference on Generics

Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Dear All,

I am facing a lot of issue in understanding Generics. Can someone point me towards a good resource over generics?

Arhaan
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Well.....Do you have any specific generic questions for us? Yes, I agree that generic is a very complicated topic in Java.

Read Mughal and Ras's SCJP study guide as a supplement can help. And KB's study guide can help too. Oracle's official Java tutorial can help too.
You may be able to download this book online:
http://www.google.com/#q=scjp+certification+Java+6+study+guide,+Mughal&hl=en&prmd=imvns&ei=NqJCT6avD-eK2QW8-JSpCA&start=0&sa=N&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&fp=d272a33775ff33c4&biw=1366&bih=598

In the Java Ranch forum, sometimes, I came across some advanced issues related to overloading or overriding generic methods or type erasure ... But since KB's book does not mention anything about this. According to KB, we don't need to worry about type erasure for this OCPJP 6.0 exam.

Well, as far as the exam is a concern, you can first focus more on List<Object> and study more about it first before you get into more details about generics.

That is just my opinion.
Arhaan Singhania
Ranch Hand

Joined: Mar 28, 2011
Posts: 89
Hi Helen,

I do have a specific question how can we dissect this



Can you elaborate what does it mean?

Arhaan
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
public static<T extends Comparable<? super T>> void sort(List<T> list )



This sort method takes a List that contains T. So, what is T? T is any type that extends Comparable<T>.
If T is not a comparable, it won't compile.

Here are my analysis:
1. During compilation of this sort method, the compiler does not know what exactly T is. If you pass List<Integer> myIntList to sort method, it will compile because Integer is a comparable.
If you pass in List<Animal> where Animal is not a comparable, it won't compile.

2. What is mean by Comparable<? super T>? I think you can treat it as Comparable<T>. Based on M&R's study guide, Comparable<T> IS-A Comparable<? super T>.
You may understand this better : T extends Comparable<T>. Integer class implements Comparable<Integer>, so as Double, String, Float....
But this one "scares" me : T extends Comparable<? super T>. I may think if T is an integer , will T extends Comparable <Object>? Comparable<? super Integer> can be assigned to Comparable<Object> type. But somehow, it does not make sense to say Integer is a type of Comparable<Object> because Integer implements Comparable<Integer>.

3.There is another important key point I want to make: Comparable<Integer> is not a Comparable<Object>.



For the exam, I guess it may not be that difficult (KB never put this type of question in their book).....I think....But it is good to learn just in case.

Can anyone verify what I write?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

During compilation of this sort method, the compiler does not know what exactly T is.

Actually, this is the entire point of generics. The compiler will know *exactly* what T is, so it can complain about it ahead of time when something is wrong.

But this one "scares" me : T extends Comparable<? super T>. I may think if T is an integer , will T extends Comparable <Object>? Comparable<? super Integer> can be assigned to Comparable<Object> type. But somehow, it does not make sense to say Integer is a type of Comparable<Object> because Integer implements Comparable<Integer>.

<T extends Comparable<? super T>> means that T can be of any type that is comparable to a supertype of T. For instance, if we have a class Fruit implements Comparable<Fruit>>, we can simply pass a List<Fruit> to the sort() method. But what if we want to sort a List<Apple>. Apple is a Fruit, which also means Apple is a Comparable<Fruit>. But this means that if <T extends Comparable<T>>, we could not use the sort method on a list of apples, because Apple is not a Comparable<Apple>. So in order to let the method accept subclasses of types that implement the Comparable interface, we use <T extends Comparable<? super T>>.
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

I would recommend reading Generics chapter from McGraw.Hill.Java.The.Complete.Reference.7th.Edition.Dec.2006. Herbert Schildt did a really good job explaining Generics concept. Highly Recommended.


MySQL Blog
http://mysqlearner.blogspot.com/
 
jQuery in Action, 2nd edition
 
subject: Good Reference on Generics
 
Similar Threads
Generics
Generics..
generics
Generics
Generics