About Question enthuware.ocajp.i.v7.2.1096 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocajp.i.v7.2.1096 :
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 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
You got it right
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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?
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Fri May 15, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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.Ad9999 wrote:I understand the rules but let me get this straight...
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.1. Integer i = new Integer(5); --- i pointing to a new Integer 5 Object
Yes. This is just a reference assignment.2. Integer j = i; --- j pointing to same new Integer 5 Object
Think of i-- as i = i-1;3. i--; --- (what's supposed to happen) i = new Integer 4
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.
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.4. i++; --- (what's supposed to happen) i = new Integer 5
Yes, they are pointing to different objects. i is from cache and j is not from cache. See comments above.5. System.out.println((i==j)); --- i and j are now pointing to different objects (false)
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.At JVM startup??
Yes, it would have to be created at some point. That point could be at startup or could be before the first usage.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Fri May 15, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 12
- Joined: Tue Nov 24, 2015 4:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
For Boolean I don't see how it can use the same wrapper class. Can you give me an example where == returns true?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
Boolean b1 = true;
Boolean b2 = true;
System.out.println(b1==b2);
Boolean b2 = true;
System.out.println(b1==b2);
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Fri Dec 04, 2015 11:27 am
- Location: Saint-Petersburg
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
I think it is like this:When you do i++, what actually happens is something like this: i = new Integer( i.intValue() + 1);
Code: Select all
i = Integer.valueOf( i.intValue() + 1)
-
- Posts: 1
- Joined: Fri Dec 18, 2015 7:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
I agree with Russtam.
The explanation provided by enthuware is misleading, because i++ translating to
implies that i and j could never refer the same object
The explanation provided by enthuware is misleading, because i++ translating to
Code: Select all
i = new Integer( i.intValue() + 1);
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1096 :
Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 44 guests