SCJP 93 % ,preparing for SCWCD
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan wrote:If the related methods arguments are in a hierarchy like your example, the method which has the most sub class reference will be invoked. And if the arguments are not in a hierarchy, then Compiler flag error. try with Integer and String.
SCJP 93 % ,preparing for SCWCD
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
SCJP6.0,My blog Ranchers from Delhi
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Dieter Quickfend wrote:Just know that
1) a null value can be assigned to any reference of any class.
2) the compiler always tries to resolve a method call to the most specific parameter
Now, when you know this, try to solve this code fragment:
What do you think will happen here?
SCJP 93 % ,preparing for SCWCD
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Dieter Quickfend wrote:No, it applied to anything. If it's ambiguous, the compiler will protest. Null is just a really easy example of this behavior, because it can be assigned to anything and everything.
SCJP 93 % ,preparing for SCWCD
Mehdi Ben Larbi wrote:
So this rule of class hierarchy only applies to a null reference ?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mehdi Ben Larbi wrote:
-Isn't it an ambiguous call ?
Mehdi Ben Larbi wrote:
-Why did it choose to run the bB(Integer b) while we had bB(object b) ? Isn't boxing supposed to be chosen after casting ?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
What do you think, Mehdi? Is an int an instance of Object? can it widen to Object?Mehdi Ben Larbi wrote:
Dieter Quickfend wrote:No, it applied to anything. If it's ambiguous, the compiler will protest. Null is just a really easy example of this behavior, because it can be assigned to anything and everything.
When i try to compile and run your example with an int it works fine and calls the Integer method,two questions here :
-Isn't it an ambiguous call ?
-Why did it choose to run the bB(Integer b) while we had bB(object b) ? Isn't boxing supposed to be chosen after casting ?
Really confused here.
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Abimaran Kugathasan wrote:
Why do you think so? Which are the method, you consider, will be ambiguous?
SCJP 93 % ,preparing for SCWCD
Dieter Quickfend wrote:
What do you think, Mehdi? Is an int an instance of Object? can it widen to Object?
can an int be boxed to a Byte without casting?
SCJP 93 % ,preparing for SCWCD
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Mehdi Ben Larbi wrote:
int can be cast to long or float,and there is no class hierarchy between long and float.
But there is no compiler error here,why ?
Primitive types maybe ?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Dieter Quickfend wrote:an int cannot widen to Object.
It can however, autobox to Integer, and an Integer can widen to Object.
But if there is a method that takes an Integer, and int boxes to Integer... why would it widen to Object?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Dieter Quickfend wrote:an int cannot widen to Object.
It can however, autobox to Integer, and an Integer can widen to Object.
But if there is a method that takes an Integer, and int boxes to Integer... why would it widen to Object?
Abimaran Kugathasan wrote:
Primitive type are subjected to widening. There is no class for primitive types, then where is you class hierarchy come from?
SCJP 93 % ,preparing for SCWCD
- Primitive widening uses the "smallest" method argument possible
- Used individually, boxing and var-args are compatible with overloading
- You CANNOT widen from one wrapper type to another (IS-A fails)
- You CANNOT widen and then box (an int can't become a Long)
- You can box and then widen (An int can become an Object, via Integer)
- You can combine var-args with either widening or boxing
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Mehdi Ben Larbi wrote:
I suppose it's the same for all primitive types ?
For example float autobox to Float then widen to Object ?
Mehdi Ben Larbi wrote:
Ok so this rule only applies to Objects ?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Dieter Quickfend wrote:I'll recount you the most important rules for autoboxing, from Kathy & Bert's SCJP Study Guide (please forgive me and feel free to edit this out if I'm plagiarizing or something):
- Primitive widening uses the "smallest" method argument possible
- Used individually, boxing and var-args are compatible with overloading
- You CANNOT widen from one wrapper type to another (IS-A fails)
- You CANNOT widen and then box (an int can't become a Long)
- You can box and then widen (An int can become an Object, via Integer)
- You can combine var-args with either widening or boxing
I hope that settles it.
Abimaran Kugathasan wrote:
Which rule you are talking about?
SCJP 93 % ,preparing for SCWCD
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Dieter Quickfend wrote:Null is just a really easy example of this behavior, because it can be assigned to anything and everything.
SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar wrote:
No,It is wrong,
we cannot assign null to a primitive type
like we cannot do like this,
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
|