Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper's immutability

 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer i1=a value between 0 to 127;
Integer i2=a value between 0 to 127;

when checked for reference giving result "true"

but when that range is outside that one then result is "false".

so i want to know why so small range is provided" ?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to note, the range of cached Integers is actually configurable in newer versions of JRE and might differ between various JVM implementations.

How large should the cached range be in your opinion? Why do you think that 0 to 127 is "small"?
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:Just to note, the range of cached Integers is actually configurable in newer versions of JRE and might differ between various JVM implementations.

How large should the cached range be in your opinion? Why do you think that 0 to 127 is "small"?



sir does caching not happen when we write
Integer i=10;
Integer j=10;
because autoboxing will be happen for both automatically (after then both would be object). they why they do not show false when comapred. ?

actually i m confused which i should prefer
Integer i=10 or Integer i=new Integer(10);
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod Kumar Pandey wrote:sir does caching not happen when we write
Integer i=10;
Integer j=10;
because autoboxing will be happen for both automatically (after then both would be object).

Yes, caching happens, and yes, Autoboxing happens automatically. Autoboxing will take advantage of the Object Cache. The cache is there to reduce the number of objects created for the most common ranges of values, for various definitions of 'common' (so the range shouldn't be assumed as Martin said). See CachedObjects.

they why they do not show false when comapred. ?
Because they use the cache, which gives each variable a reference to the same Object.

actually i m confused which i should prefer
Integer i=10 or Integer i=new Integer(10);

Prefer the first, but never ever ever compare with == when using Objects*. See AvoidTheEqualityOperator.

* Well, almost never ever ever.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49787
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod Kumar Pandey wrote: . . . which i should prefer
Integer i=10 or Integer i=new Integer(10);
Neither. You should useIt is all explained in the documentation.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But autoboxing does exactly the same as Integer.valueOf(), doesn't it? (At least the tutorial says so.) Why should one be preferred over the other?
 
Winston Gutkowski
Bartender
Pie
Posts: 10504
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:But autoboxing does exactly the same as Integer.valueOf(), doesn't it? (At least the tutorial says so.) Why should one be preferred over the other?

I think it's a matter of taste. Personally, I'm old school, so I tend to call valueof() explicitly most of the time; but the fact of the matter is that if you have a method like:
public static final Double average(Integer... values) { ...
there's absolutely nothing to stop someone calling it with
double avg = average(1, 2, 3, 4, 5);
and in such a case, I'd probably do the same because the valueOf()'s just clutter up the code.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49787
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/the question was which should I prefer, and the correct answer to the preference was valueOf. Preferences do not apply in 100% of situations.
 
Pramod Kumar Pandey
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i saw the documentation and i found a line says ...
public static Integer valueOf(int)
This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

in the last word it said may cache other values outside of this range ...how it can be proved ? sir.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see if a particular value is cached, if you for some reason find that important, like this:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49787
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More discussion about how many Integer values can be cached here. It tells you how to change the range, but I think (not certain) that only applies to positive values.

Somebody pointed out I was mistaken earlier. Sorry Using autoboxing will produce an Integer object, which is cached in the range -128…127. So you can use Integer i = 123; all right. The details are in the Java Language Specification. It says something slightly different about values outside the range -128…127. I suspect that is not significant, however. You can test that by checking whether Integer.valueOf(123) == (Integer)123
One seems to use these options to alter the cache max: java -Xint -XX:AutoBoxCacheMax=256 Foo
I found that here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic