GeeCON Prague 2014*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Primitive and unboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Primitive and unboxing" Watch "Primitive and unboxing" New topic
Author

Primitive and unboxing

N Sam
Ranch Hand

Joined: Nov 23, 2013
Posts: 77
    
    2
This question is from the Esteban Herrera - Java Practice Questions book.

The pertaining code is given below. The answer given for question 6.15 states .... "m1 receives a primitive value, so the object is unboxed.....".

The above explanation in the answer seems to imply "only because m1 parameter is primitive type, an unboxing is done, and the print out of above code is false". But, i tried running the same code with one modification: the parameter to m1 is the object Boolean b. Now, the argument passed from main becomes an object reference, because of public void m1(Boolean b). Why is this not changing the value of variable b (in main) to true ? (I ran the code and the result is the print out 'false'). Why is the object reference passing not giving the changed result, in this case ?

@Estaban - Anyways, it seems from the above that the answer to this question should not imply ".....m1 receives a primitive value and thus.....", because the result is same in both cases.

@moderator - please don't mark this thread resolved. I will mark it once i finish the book.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

Autoboxing and unboxing is not on the OCAJP7. So this question should definitely not be in the book (unless you have the version for the OCPJP7).

The autoboxing/unboxing only makes sure this code is able to compile (which would not be the case in a JDK prior to version 1.5). It has nothing to do with why the code prints false. Use a String instead of a boolean (to get rid of the unboxing/autoboxing) and see what happens....


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Mouad Morabit
Greenhorn

Joined: Mar 15, 2014
Posts: 7

Hi, The m1() method takes a primitive boolean argument not a Boolean wrapper (object that stores the reference ) , so the value changes only in the m1() method but in the main the "b" keeps the same value :/

With Autoboxing and Unboxing you don't have to worry if the argument si a "boolean" or "Boolean" and vice verca


OCAJP7
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

Mouad Morabit wrote:Hi, The m1() method takes a primitive boolean argument not a Boolean wrapper (object that stores the reference ) , so the value changes only in the m1() method but in the main the "b" keeps the same value :/

Even if it was a boolean wrapper (instead of the primitive) or a String or any other object, the output would be similar!
Mouad Morabit
Greenhorn

Joined: Mar 15, 2014
Posts: 7

Ouch sorry I see, I guess even if you use a Wrapper boolean, when you assign a new value in the method body, the reference changes, and a new object is created
Esteban Herrera
author
Greenhorn

Joined: Dec 25, 2004
Posts: 21

Hi,

This question relates to the objective "Determine the effect upon object references and primitive values when they are passed into methods that change the values", though maybe I shouldn't had used the concept of autoboxing...

Anyway, N Sam your doubt seems to be why even when m1 takes an object reference (Boolean) the value is still false in main, right?

Well, I think something different happens when the parameter is an object. Mouad is right, but let me elaborate a bit more on his answer.

In Java we have the concepts of a reference and an object. In an assignment, the left-side defines the reference and the right-side defines the object. They are different things.

When a method takes an object as a parameter, Java makes a copy of the reference so the method can use the object using this new reference. This has the following implications:
- This reference is only used in the method and if it's not saved, it's lost once the method is finished.
- If you make a change to the object using this reference, since it's the same object you’re referring to in the caller and called method, the changes are persisted.
- If you assign a new to object to the method's reference, changes are not reflected in the original object (since it's another object you're working on now).

And this is what's happening when m1 takes a Boolean:
In main we have:
b(reference 1) -> false (object 1)
When entering m1:
b(reference 2) -> false (object 1)
When b = true is executed:
b(reference 2) -> true (object 2)

When m1 is done and control is returned to main, reference 2 is lost, but we still have reference 1 pointing to object 1, which is false.

I hope I made myself clear. The main lesson here is that methods take a copy of the reference (if we're dealing with objects, or a copy of the value if we're dealing with primitives) and not the real object. I think that if you have this concept in mind (even if you have to draw the references, objects and arrows), you'll answer correctly the questions related to the mentioned objective in the exam.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5300
    
  13

Esteban Herrera wrote:though maybe I shouldn't had used the concept of autoboxing...

Not maybe, but definitely. You should not use concepts which are not on the exam. It will only confuse people and make them doubt if they should learn that topic as well. And it was also not needed at all, using a String (or any other class) would have had the same result.
 
GeeCON Prague 2014
 
subject: Primitive and unboxing