| Author |
having doubt with Generics query String comparision
|
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
Dear Ranchers ,
Why is Timepass only being displayed in this scenario
|
http://www.lifesbizzare.blogspot.com || OCJP:81%
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1430
|
|
|
Well, what else did you expect given the JavaDoc description of how String#compareTo() is implemented?
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
Jelle Klap wrote:Well, what else did you expect given the JavaDoc description of how String#compareTo() is implemented?
In that javadoc description its mentioned that class String implements Comparable, but in one of the videos >>Click Here<< in a method it extends Comparable??? is that video correct and does it have reliable information
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2385
|
|
Comparable is an interface. An interface can extend (but not implement) another interface, and a class can implement (but not extend) an interface. The correct expression therefore is "The String class implements the Comparable interface".
So yes, Javadoc has it right. Unsurprisingly I didn't try to asses the video, I don't see any upside of presenting this information as a video. Having this kind of information in a textual form seems much more practical to me.
|
 |
Vishal Hegde
Ranch Hand
Joined: Aug 01, 2009
Posts: 984
|
|
it was a generic example in that video showing as
does that means T is an interface in this scenario ..i mean if i use int like max(1,2,3) will T act as What?
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1430
|
|
The semantics of the extends keyword in the context of defining a type parameter are somewhat different.
Here extends can be applied to any type, whether that is a class or an interface.
|
 |
Martin Vajsar
Bartender
Joined: Aug 22, 2010
Posts: 2385
|
|
T is a formal type parameter (so something different from both a class and an interface). When you call this method, you'll provide a"value" for this parameter (in a process called generic type invocation), the "value" can be either yet another type parameter, or a concrete type - class or interface.
The T extends Comparable<T> is a way of limiting the possible types to be used as a "value" of T when calling this method to types which implement (when it is a class) or extend (when it is an interface) the Comparable interface. It is therefore a bounded type parameter. In other words, it makes sure that you can call the compareTo() method on a, b and c parameters, or can pass these parameters to functions which require a Comparable instance.
You might also want to read the Generics tutorial.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3865
|
|
T might be an interface, but it doesn't have to be.
In that particular example, T must be something you can assign an int to that extends or implements Comparable<T> (so it's a bit self-referential). There's only one possible T that matches those conditions: Integer (which implements Comparable<Integer>).
|
 |
 |
|
|
subject: having doubt with Generics query String comparision
|
|
|