• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper's immutability

 
Ranch Hand
Posts: 52
Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" ?
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic