This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is-A Relationship and Autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is-A Relationship and Autoboxing" Watch "Is-A Relationship and Autoboxing" New topic
Author

Is-A Relationship and Autoboxing

Ryan Slominski
Ranch Hand

Joined: Nov 05, 2010
Posts: 33
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."
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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 - http://www.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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Joined: Nov 05, 2010
Posts: 33
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

Joined: May 13, 2002
Posts: 180
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

Joined: Nov 05, 2010
Posts: 33
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

Joined: May 13, 2002
Posts: 180
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

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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."

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

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:


luck, db
There are no new questions, but there may be new answers.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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

Joined: May 13, 2002
Posts: 180
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

Joined: Aug 31, 2010
Posts: 394

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

Joined: May 13, 2002
Posts: 180
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

Joined: Aug 31, 2010
Posts: 394

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

Joined: May 13, 2002
Posts: 180
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

Joined: Aug 31, 2010
Posts: 394

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

Joined: May 13, 2002
Posts: 180
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

Joined: Aug 31, 2010
Posts: 394

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

Joined: May 03, 2008
Posts: 4530
    
    5

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.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

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


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Joined: May 13, 2002
Posts: 180
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

Joined: May 03, 2008
Posts: 4530
    
    5

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

Joined: May 13, 2002
Posts: 180
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

Joined: Aug 31, 2010
Posts: 394

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

Joined: Oct 15, 2010
Posts: 281

@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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is-A Relationship and Autoboxing