Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Nov 12, 2012 5:49 pm
by ETS User
Either I am seriously not getting it, or else there is an error here and the authors of this question intended to use at least one variable of type byte instead of both being int or Integer. Why else would the assignments back and forth be limited to 127 (the max byte value)?

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Nov 12, 2012 5:59 pm
by Guest
Aha, type first, think later, that's my motto. I am re-reading the explanation about Java "re-using" the wrapper objects for values up to 127 -- is that the answer? This seems really obscure to me. At any rate, if ...

wrapper2 = wrapper1;
wrapper1--;
wrapper1++;
System.out.println(wrapper2 == wrapper1);

if I understand this right, then because of this re-use-the-wrapper-object feature, I will get true if wrapper1 started out <= 127, but false otherwise? Holy moly.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Nov 12, 2012 6:17 pm
by admin
You got it right :)

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Nov 12, 2012 6:31 pm
by Guest
Thanks, but I have confused myself further! It turns out that I get different results when assigning the value different ways.

Integer x = new Integer(127);
Integer y = x;
x--;
x++;

evaluates (x == y) to false.... whereas....

Integer x = 127;
Integer y = x;
x--;
x++

evaluates (x==y) to true!!!! What the heck? Is there something about boxing/unboxing instead of using the Integer constructor? Does "Integer x = 127" tell the compiler, hey, you are only using eight bits here, so feel free to reuse that danged wrapper object, whereas new Integer(val) doesn't?

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Nov 12, 2012 7:26 pm
by admin
When you do new Integer(127), there is no "auto" boxing. You are boxing it yourself. Effectively, you are asking the JVM to create a new object instead of reusing an existing one.
However, x++ or x-- uses auto-boxing and so that causes the JVM to assign an interned object, which is different from the one that you created earlier.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Nov 12, 2012 10:16 pm
by Guest
Makes perfect sense; thank you.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Thu Jan 24, 2013 1:58 am
by icepeanuts
Such thing is very tricky. So I prefer to program in an effective and safe way. For example, if int is enough, I would not use Integer. If I want to have access to the int value of an Integer, I would use intObj.intValue().

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Thu Feb 21, 2013 3:00 pm
by Guest
Explanation says Java re-uses all wrapper objects for Character values from decimal 0 to 127.

I tried

Character i = new Character('1');
Character j = i;
i--;
j++;
System.out.println(i==j);

and it printed false.
Could you please explain?
Thanks

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Thu Feb 21, 2013 3:05 pm
by admin
Explained above:
When you do new Integer(127), there is no "auto" boxing. You are boxing it yourself. Effectively, you are asking the JVM to create a new object instead of reusing an existing one.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Thu Feb 21, 2013 3:28 pm
by Guest
Got it,
Thanks.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Wed Jul 01, 2015 12:18 am
by Ad9999
I understand the rules but let me get this straight...

1. Integer i = new Integer(5); --- i pointing to a new Integer 5 Object
2. Integer j = i; --- j pointing to same new Integer 5 Object
3. i--; --- (what's supposed to happen) i = new Integer 4
4. i++; --- (what's supposed to happen) i = new Integer 5
5. System.out.println((i==j)); --- i and j are now pointing to different objects (false)

However! From testing, I have come to the conclusion that in line 3 and 4 NO new objects are being created. At JVM startup?? Integer -128 to 127 objects(and others) are automatically created and constantly re-used unless explicitly told to create a new object.

Basically my point is, the explanation uses the word "re-uses" but that would mean they would have to be created first at some point in order to be RE-used.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Wed Jul 01, 2015 1:45 am
by admin
Ad9999 wrote:I understand the rules but let me get this straight...
Basically, new Integer objects are created for values ranging from –128 to +127, only when you use the "new" keyword to create them. Otherwise, cached Integer objects are used. For other values, there is no cache.
1. Integer i = new Integer(5); --- i pointing to a new Integer 5 Object
Yes. Because of the usage of new keyword, even if an Integer object containing 5 already exists in the cache, a new object will be created. If an Integer object containing 5 is not in the cache, this one will NOT go in the cache.
2. Integer j = i; --- j pointing to same new Integer 5 Object
Yes. This is just a reference assignment.
3. i--; --- (what's supposed to happen) i = new Integer 4
Think of i-- as i = i-1;
Since there is no new keyword here, a cached Integer object containing 4 will be assigned to i if such an object already exists in the cache. If not, a new object will be created, put in the cache, and will be assigned to i.
4. i++; --- (what's supposed to happen) i = new Integer 5
Since there is no usage of the new keyword here, an Integer object containing 5 from the cache will be used. If it does not exist in the cash, a new one will be created and put in the cache first.
5. System.out.println((i==j)); --- i and j are now pointing to different objects (false)
Yes, they are pointing to different objects. i is from cache and j is not from cache. See comments above.
At JVM startup??
Possibly, but irrelevant. You don't know and you don't need to know when exactly an object was created. You only care that it is assigned at the time the statement like Integer i = 5 or i++ is executed.
Basically my point is, the explanation uses the word "re-uses" but that would mean they would have to be created first at some point in order to be RE-used.
Yes, it would have to be created at some point. That point could be at startup or could be before the first usage.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Fri Jul 03, 2015 9:22 am
by Ad9999
Ty for the detailed response! Although I am still curious as to when they are actually created, but that is out of scope of this exam. :p

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Fri Jul 03, 2015 10:23 am
by admin
One could develop a JVM that creates these object at startup or on demand. Both options are valid. To find which option Oracle's JVM uses, you may need to go through their documentation.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Wed Nov 25, 2015 2:15 pm
by dannysantos1985
For Boolean I don't see how it can use the same wrapper class. Can you give me an example where == returns true?

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Wed Nov 25, 2015 6:50 pm
by admin
Boolean b1 = true;
Boolean b2 = true;
System.out.println(b1==b2);

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Dec 07, 2015 11:59 am
by Russtam
When you do i++, what actually happens is something like this: i = new Integer( i.intValue()  + 1); 
I think it is like this:

Code: Select all

i = Integer.valueOf( i.intValue()  + 1)
because constructor always create new object, and only valueOf method use cache.

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Fri Dec 18, 2015 7:14 am
by lanfear
I agree with Russtam.
The explanation provided by enthuware is misleading, because i++ translating to

Code: Select all

i = new Integer( i.intValue()  + 1); 
implies that i and j could never refer the same object

Re: About Question enthuware.ocajp.i.v7.2.1096 :

Posted: Mon Dec 21, 2015 3:59 am
by admin
Fixed.
thank you for your feedback!