• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't reproduce result from book  RSS feed

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
I can't reproduce the same output from the book (study guide for scjp6 bates/sierra - page 246):
This is my code:
Integer a = 2;
Integer b = 4;
Integer c = Integer.valueOf("3");
Integer d = Integer.valueOf("7");
if(a == b) System.out.println("same object");
if(c == d) System.out.println("same object");

According to the book the result of the first if-query would be true (since wrapper objects constructed through boxing will be == for values smaller than 127.
I don't get any output.
Any ideas why? (I have Java 7 btw)
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you sure that is correct? because a and b are NOT pointing to the same object if one has a value of 2 and the other a value of 4...
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bora,

fred has already replied, but my guess is: the point here is, if the values are actually same, then objects are also same (if the values are less than 127).

E.g.

then a==b is true
But if

then a==b might not be true (in fact it will be false almost all the time - though I'm not 100% sure if it will be always false)

I hope this helps.
 
Bora Sabrioglu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, there you go... that was the mistake... thank you guys...
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote: . . .
then a==b might not be true (in fact it will be false almost all the time - though I'm not 100% sure if it will be always false)
. . .
The general contract for Integer#valueOf permits that to return true.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The general contract for Integer#valueOf permits that to return true.

Exactly. My only point was: caching is performed only for values in range -128 to 127. For other values (e.g. > 127), a==b will return false.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote: . . . For other values (e.g. > 127), a==b will return false.
That is what most people think, but it is not what the documentation says. It may in fact return true. I cannot remember how the option is set, however.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That is what most people think, but it is not what the documentation says. It may in fact return true. I cannot remember how the option is set, however.

This is what I wanted to know
And that is the reason I mentioned 'though I'm not 100% sure if it will be always false' in my previous post

Thanks Campbell!
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Anayonkar Shivalkar wrote: . . . For other values (e.g. > 127), a==b will return false.
That is what most people think, but it is not what the documentation says. It may in fact return true. I cannot remember how the option is set, however.


Javadoc of Integer#valueOf says:
Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.


I googled that using -XX:AutoBoxCacheMax=<size> option can increase the size of cache.
The same is stated on javadoc of Integer.IntegerCache class.

But on my machine I got:
Unrecognized VM option 'AutoBoxCacheMax=255'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.


I don't know why.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is more about auto box cache here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!