• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Exam Lab Test 2 Question

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

I am reviewing my answers to exam lab Test2 and there is an answer i can't understand,even with the explanation :

Here is the question :


The answer is : Integer In

I don't even know how to start thinking to solve this,i was sure it would raise a compiler error but it's working fine.
Any good explanation please ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thanks.
Where does this rule come from ? Is it specific to invocation with null ?
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here it is
java.lang.Object
|
+--java.lang.Number
|
+--java.lang.Integer


so most subclassed version will be invoked will be called
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well here hierarchy is
Object
Number
Byte and Integer


Now here
as methods with Byte and Integer are defined the call to the method is ambiguous
the compiler cannot decide whether to cal method with Integer or Byte

so compiler error will be shown

 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



I think it will provoke a compiler error because Byte and Integer are at the same level,no hierarchy between them so it's ambiguous.

Edit : I see you've already answered your question,i'am happy to see that i understood your explanation,thanks.
So this rule of class hierarchy only applies to a null reference ?
Because when i try to call the method with a String reference it works fine.
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mehdi Ben Larbi wrote:
So this rule of class hierarchy only applies to a null reference ?


Why don't you try to come up with your own example? Check the below code!~
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mehdi Ben Larbi wrote:
-Isn't it an ambiguous call ?


Why do you think so? Which are the method, you consider, will be ambiguous?

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 ?



Which is the most sub class in the hierarchy? So, which would be invoked?
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

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?
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:
Why do you think so? Which are the method, you consider, will be ambiguous?



Thanks for the example,i understand the concept now.
Can't directly unbox form int to Byte so no ambiguous call here.
Check out this example :



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 ?
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Yes it can.
When i write Object o=int reference it works perfectly.
I remember reading in the SCJP guide that the JVM would first choose widening then casting then var-args so why did it choose Integer over Object ?
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?


Primitive type are subjected to widening. There is no class for primitive types, then where is you class hierarchy come from?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


If there is no method with the argument Integer, then the method with Object as argument will be invoked.
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



I suppose it's the same for all primitive types ?
For example float autobox to Float then 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?



Ok so this rule only applies to Objects ?
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mehdi Ben Larbi wrote:
I suppose it's the same for all primitive types ?
For example float autobox to Float then widen to Object ?


Yes, don't refere the book yet?

Mehdi Ben Larbi wrote:
Ok so this rule only applies to Objects ?


Which rule you are talking about?
 
Mehdi Ben Larbi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Thanks a lot Dieter,it's clear now.

Abimaran Kugathasan wrote:
Which rule you are talking about?



Class hierarchy when invoking an overloaded method.
It's doesn't apply to primitives.


Anyway thanks again Dieter and Abimaran,you helped me a lot,everything is clear now.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome!
 
Dieter Quickfend
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dieter Quickfend wrote:Null is just a really easy example of this behavior, because it can be assigned to anything and everything.


No,It is wrong,
we cannot assign null to a primitive type
like we cannot do like this,

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shanky Sohar wrote:
No,It is wrong,
we cannot assign null to a primitive type
like we cannot do like this,


He mentioned about reference variable, not primitive variables!
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic