aspose file tools*
The moose likes Beginning Java and the fly likes Wrapper's immutability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper Watch "Wrapper New topic
Author

Wrapper's immutability

Pramod Kumar Pandey
Greenhorn

Joined: Dec 05, 2013
Posts: 29

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

Joined: Aug 22, 2010
Posts: 3434
    
  47

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
Greenhorn

Joined: Dec 05, 2013
Posts: 29

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

Joined: Jan 28, 2003
Posts: 3946
    
  17

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.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Aug 22, 2010
Posts: 3434
    
  47

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

Joined: Mar 17, 2011
Posts: 7035
    
  16

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

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
/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
Greenhorn

Joined: Dec 05, 2013
Posts: 29

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
Bartender

Joined: Oct 14, 2005
Posts: 18121
    
    8

You can see if a particular value is cached, if you for some reason find that important, like this:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper's immutability
 
Similar Threads
100 == 100 but 1000 != 1000 - what's going on? [boxing, ==, and equals()]
Boxing == Long
Object comparasion question
short and Integer
type casting error!