"If the facts don't fit the theory, get new facts" --Albert Einstein
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Florin Florentin wrote:
One more question.
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
BEE MBA PMP SCJP-6
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Florin Florentin wrote:Thank you for your quick replies and you help Mark. You helped me .
One more question. In this case,
the code compiles, so why here the second argument must not be a supertype of the first type? Maybe it's a silly question but i don't know why.
Jim Hoglund wrote:Try this. The compiler error generated shows that 'T' is set to type Object. Jim ... ...
Mark Moge wrote:Ok I was wrong. And after some thinking ... in my opinion it works like this: if you have a generic declaration of a method
T from a metod works as T extends superclass of all arguments in method.
So you can put any type as argument:
Kevin Kilbane wrote:
When the arguments are of the same type, the generic type is that type
Kevin Kilbane wrote:
When more than one argument is passed to a method that defines a generic type and those arguments are not all of the same type then the compiler treats the generic type as Object
Kevin Kilbane wrote:
1. If the arguments are all of the same type then the compiler treats T as that type e.g. if all arguments are Strings then T is treated as a String. Simple.
2. If the arguments are of different types but are in the same class hierarchy then the compiler treats T as the highest in the hierarchy e.g. if the arguments are of types Integer and Number then T is treated as Number.
Kevin Kilbane wrote:
3. If the arguments are of different types and are NOT in the same class hierarchy then the compiler treats T as Object e.g. if the arguments are of types Integer and String then T is treated as Object by the compiler.
BEE MBA PMP SCJP-6
What are you doing in my house? Get 'em tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|