• 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

Is-A Relationship and Autoboxing

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The LearnKey MasterExam practice test B question 52 (Bundled with SCJP for Java 6 Study Guide book by Kathy Sierra and Bert Bates) asks to check all that are true and reports the following as a correct answer:

D In some cases, is-a relationships are used in the process of autoboxing.



This seems like a really bad answer. The explanation given is:

... you can autobox an int to a Number (via an Integer).



This expanation is describing an autobox followed by a widening, which can occur with method matching. The widening and autoboxing are two independent things, which don't depend on each other. However, a method matching process may depend on each to occur. Perhaps the statement should read something like "In some cases, is-a relationships are used in the process of method matching."
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In some cases, is-a relationships are used in the process of autoboxing.



Hi Ryan. I did not understand what you meant by method matching. As far as autoboxing / unboxing goes primitives and their wrappers are converted between each other. The class objects that hold them are not associated in any way. So when the question is viewed in that angle, the answer is that there is no is-a relationship involved in auto-unboxing.

The other way to look at it is as defined in this post - https://coderanch.com/t/518178/java-programmer-SCJP/certification/relationship

Henry has provided an example in that link above. Viewed in that perspective you *could* say that is-a relationship was involved in an autoboxing
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both statements are correct and can be applied to overloading where the JVM attempts to find the right method match with reference to declared parameters, and then invokes the method if found:

1) The statement "In some cases, is-a relationships are used in the process of method matching." is correct. For example, the method call s(new Integer(1)) will match the method declaration void s(Number n) {} due to a widening conversion from Integer to Number.

2) The statement "In some cases, is-a relationships are used in the process of autoboxing." is also correct. For example, the method call s(1) will match the method declaration void s(Number n) {} due to autoboxing of int to Number. This is a special case scenerio where autoboxing involves not only boxing an int to an Integer, but also involves a widening conversion to a Number.

It's a deceptive question but most exams are designed to be deceptive.
 
Ryan Slominski
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:I did not understand what you meant by method matching.



Method matching is the process of determining which overloaded method to call. For example which method gets called:



Deepak Bala wrote:Henry has provided an example in that link above. Viewed in that perspective you *could* say that is-a relationship was involved in an autoboxing



The example is again of autoboxing followed by a widening, which are two independent things. In his example, method matching isn't used; a method is simply boxing and then widening and returning the result.

ogeh ikem wrote:This is a special case scenerio where autoboxing involves not only boxing an int to an Integer, but also involves a widening conversion to a Number.



This is actually two separate things: autoboxing and then widening. That is my point: widening is NOT part of autoboxing so saying that inheritence plays a role in autoboxing is plain wrong.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autoboxing is a widening conversion.

Before Java 5, widening conversion for primitive types couldn’t be mixed with widening conversion for reference types i.e. they were two separate things.

In Java 5, widening conversion for primitive types can be mixed with widening conversion for reference types. This is called autoboxing.

Autoboxing is defined as treating a primitive type as a reference type without any extra source code. The compiler automatically supplies the extra code needed to perform the widening conversion. For example, the compiler automatically supplies the extra code needed to perform the widening conversion of 1 to Number. 1 being the primitive type and Number being the reference type. Hence the statement "In some cases, is-a relationships are used in the process of autoboxing." is correct.

Due to autoboxing, you can write this code



and the compiler automatically supplies this extra code

 
Ryan Slominski
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ogeh ikem wrote:Autoboxing is a widening conversion.



Umm... no?

Autoboxing is simply converting a primitive to an object or object to a primitive automatically: the compiler inserts the primitive-to-wrapper method call (for a box), or wrapper-to-primitive method call (for an unbox) automatically. For eample, Integer.valueOf for a box or Integer.intValue() for an unbox.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just said that

Autoboxing is simply converting a primitive to an object



You're correct. The emphasis is on primitive to object. This includes converting int to Integer, int to Number or int to Object. The following examples all demonstrate autoboxing

 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys...@Ryan maybe Ogeh made a simple mistake (like any of us ) by directly defining autoboxing as a widening conversion, but if you read further you will notice the explanation as clearly meant to be widening conversion can also take place during autoboxing.

Regards

Ikpefua
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Autoboxing can be seen as a widening conversion because a smaller type can be converted to a larger type without the need for explicit casting. It can also be seen as a narrowing conversion because a larger type can be converted to a smaller type without the need for explicit casting.

The following two statements are widening conversions, however, the last one is also called autoboxing.



If you attempt to autobox the number 1 into a Double wrapper, the compiler says "Type mismatch: cannot convert int to Double". This implies that the attempted widening conversion (autoboxing) from int to Double has failed.



Similarly, if you attempt the following narrowing conversion, the compiler says "Type mismatch: cannot convert double to long".



Once again, to answer the original question posted by Ryan about the LearnKey MasterExam, during the process of autoboxing the number 1 into a Number wrapper, an is-a relationship will be used in deciding whether this autoboxing will succeed or fail. This is what the LearnKey MasterExam means when it says "In some cases, is-a relationships are used in the process of autoboxing."

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a lot of variously worded remarks in this thread about "autoboxing of an int to Number". Autoboxing always results in an object of the wrapper class corresponding to the primitive value. Assigning the result of autoboxing to a Number variable is just an upcast and does not change the class of the autoboxed object to Number.

To illustrate what I mean, run these lines of code:
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ogeh Ikem wrote: Autoboxing can be seen as a widening conversion



The above statement is simply INCORRECT according to the official documentation, there is NO definition that DIRECTLY identifies autoboxing with widening conversion you can consult this link API Documentation
That said, if its your point of view its okay...We all have the right to our point of views...

Regards

Ikpefua
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I said it can be seen as. I didn't say it is defined as

When I defined autoboxing earlier, I said

Autoboxing is defined as treating a primitive type as a reference type without any extra source code.



What I'm saying is that a widening conversion can occur during autoboxing, for example autoboxing an int into a Number wrapper in order to exploit polymorphism.

Anyway, the main issue here is the correctness of the statement made in the LearnKey MasterExam i.e. "In some cases, is-a relationships are used in the process of autoboxing." If you attempt to autobox a boolean into a Number wrapper, the compiler says "Type mismatch: cannot convert from boolean to Number".



This implies that the compiler sees the wrapper as a Number wrapper and will use an is-a relationship in the process of autoboxing an int into a Number wrapper.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ogeh Ikem wrote:What I'm saying is that a widening conversion can occur during autoboxing



This is where we coincide in terms of point of views...

Regards

Ikpefua
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So do you think that the statement made in the LearnKey MasterExam Practice is wrong?

In some cases, is-a relationships are used in the process of autoboxing



The only way that the statement can be right is if a widening conversion occurs when autoboxing an int into a Number wrapper. The explanation given by the LearnKey MasterExam Practice is

You can autobox an int to a Number (via an Integer)

 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ogeh...Now lets clarify this further...I would prefer us to call it an "Up-Casting" rather than a widening conversion because in java wrappers CANNOT be widened to each other even if they have an inheritance relationship...What really happens is an UP-CASTING.
There is NO conversion because the wrapper that the primitive is autoboxed to NEVER changes.

Please I invite you to run Darryl Burkes code:

Output:

With the above result you can see that WIDENING CONVERSION did not take place.

Summary

-Autoboxing is simply converting a primitive to an object .
-It CANNOT be seen as a widening conversion
-It is NOT a widening conversion
-you CANNOT widen and box
-UPCASTING takes during autoboxing

Regards

Ikpefua
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that this statement is accurate

in java wrappers CANNOT be widened to each other even if they have an inheritance relationship



Any subclass to superclass conversion is a widening conversion.

Please read this page for the definition of upcasting http://mindprod.com/jgloss/upcasting.html

It says that:

The official term used in the Java Language Specification for an implied upcast is widening reference conversion.



 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ogeh Ikem wrote:Any subclass to superclass conversion is a widening conversion.


@Ogeh you said "widening-conversion" the JLS says "widening-reference-conversion" have you noticed the difference in the wording? it further confirms what I said that the wrappers themselves are NOT widened to each other rather it is their references.
My point of view is that widening conversion does NOT take place between the wrappers it is their references. The JLS definitions are NEITHER a final evidence that wrappers are converted to each other AND let me inform you that the JLS is even known to have erratas. The BEST prove you have is the javac and the jvm.
The output of Darryl Burkes code snippet cleary reveals that there is NO conversion between the wrappers...and that coincides with what I said

in java wrappers CANNOT be widened to each other even if they have an inheritance relationship

What happens is a reference variable upcasting which the JLS identifies as widening-reference-conversion. Your point of views are respected even if they arent always compatible with mine...

Regards

Ikpefua
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course when I talk about widening conversion, I'm talking about references; I'm not talking about runtime objects. Runtime objects cannot spontaneously change from one type to another; only their references can. In the following widening conversion example, it is not the Integer object itself that gets widened, it is its reference



The method call n.getClass() will return Integer not Number because getClass() returns the type of the runtime object, not the type of its reference, so Darryl Burkes code is correct but it doesn't prove that widening conversion didn't take place because getClass() cannot be used to verify widening conversion. Widening conversion has taken place because the reference to the Integer has been widened to a larger type.

You still haven't answered the question I asked earlier

So do you think that the statement made in the LearnKey MasterExam Practice is wrong?


 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The learnKey master exam practice -or else otherwise proven- is NOT wrong, my diagnosis here is that attending to your expressions in this thread you said autoboxing is a 'widening-conversion', and later you modified and said that 'it-can-be-seen-as-widening-conversion', I disagreed because
I did NOT associate that with up-casting if the JLS says it is widening-reference-conversion. I guess it all seems to be clearer now.

I am so brain tired now I am developing a project based on Spring architecture while arguing all day about some kind of semantic nuances...that I need to call it a day...Fantastic debate here though.

Regards

Ikpefua
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ogeh ikem wrote:Runtime objects cannot spontaneously change from one type to another; only their references can.



Nope. References can't 'spontaneously' or otherwise change from one type to another any more than objects can.

A reference of any assignment-compatible type can be declared and assigned, with explicit or implicit casting.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is there any good link on rules of widening and autoboxing , which one takes preference over another ?
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good luck with your Spring Project.

Runtime objects cannot spontaneously change from one type to another; only their references can



What I'm saying here is that a reference to one type of object can be modified to reference a different type of object; except the reference is declared as final. For example, a reference to an Integer object can be modified to reference a Double object.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

is there any good link on rules of widening and autoboxing , which one takes preference over another ?



SCJP 6 for Java (Kathy Sierra) discusses widening and autoboxing in Chapter 3.

During method matching, widening primitive conversion will take precedence over autoboxing.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A reference can't be 'modified'. It can be assigned a value. That value can be any object (or primitive, in the case of a reference declared with a primitive type) that is assignment-compatible with the declared type of the reference.

The type of the reference doesn't change by assigning it to refer to an object of another, assignment-compatible, type.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A reference can't be 'modified'



This is not accurate, except you have a different definition for 'modified' than I do. In my book, anything that is in a different state than it was originally declared, has been modified. Java has a final keyword for this reason. Final references can't be modified but non-final references can be modified. For example, a non-final reference with memory address 001ABC can be modified to a reference with memory address 002DEF. Similarly, a non-final int variable with the value 1 can be modified to an int variable with the value 2.

The type of the reference doesn't change



This is correct.
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys I have just woken up after a 6hrs rest... ...@Ogeh thanks for your wishes...You know I just discovered a relatively serious mistake in the project that will take me about an hour to correct.
I noticed is that we are all practically saying the same thing in different terminologies. @Darryl...When we talk about reference modification we didnt mean "syntactically", we meant "semantically"...Its obvious that there is a battle in linguistic, interpretation and technical expressions here .

is there any good link on rules of widening and autoboxing , which one takes preference over another ?


IMHO the most reliable rules (regarding the above mentioned) can be found when you get hold of javac, jvm and code. The results you get from the coding resolves any doubts.

Regards

Ikpefua
 
Javin Paul
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ikpefua Jacob-Obinyan , yes that 's a sure shot method but with that finding all scenario will be difficult because no body remembers that and I am sure some one has already done the hard work, so instead of reinventing the wheel let's benefit from there knowledge but in my opinion combination of both would be best.
 
reply
    Bookmark Topic Watch Topic
  • New Topic