• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Good Reference on Generics

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

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

Arhaan
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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>>.
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic