This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Query: In boxing why == & != both works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query: In boxing why == & != both works" Watch "Query: In boxing why == & != both works" New topic
Author

Query: In boxing why == & != both works

raul saini
Greenhorn

Joined: May 18, 2010
Posts: 18
For e.g.:



In this both == & != works.

How?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

In this here is only first condition is true, not second one


Life is easy because we write the source code.....
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Raul - what do you mean by both == and != works ?
Are you saying that code prints
Equal
Different


Joanne
raul saini
Greenhorn

Joined: May 18, 2010
Posts: 18
Actually it was given in Kathy sierra SCJP book page no. 254-256, that:




Produces the output:
different objects
meaningfully equal

And,




This example produces the output:
same object
meaningfully equal

But when I executed != failed, But I'm still not able to get what the book meant.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3415
    
  12
Note the difference in the values of the Integers.
What the example is trying to show is how Integer values between -127 and 128 are cached, but values outside that range are not*.

Search the forums. This question crops up a lot.







*Actually they can be but it is not mandatory.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

This is because of how autoboxing and the Integer class work.

Autoboxing means that the compiler automatically replaces code like this:

with the following:

You know that using == on non-primitive values returns true only if the values on both sides of the == operator refer to the exact same object. (It returns false if you have two different objects, even if those objects have the same value).

So, the question now is, why does this print true:

but why does this print false:


The answer is in the way that Integer.valueOf(...); works. This method does not always return a new Integer object - instead, it manages a cache that contains Integer objects with all the values between -128 and 127. If the value you pass into the method is in that range, it will return the pre-created object in the cache. In the first example with the value 42, the variables i1 and i2 will therefore refer to the same Integer object (that comes from the cache). In the second example with the value 1000, you will get two distinct Integer objects, because 1000 is outside of the range of the cache.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anant Jagania
Ranch Hand

Joined: Oct 20, 2004
Posts: 49
Does autoboxing work the similar way for other primitive wrapper objects? Does caching happen for other wrapper objects like Long, Double, Boolean, Short, Float, Byte?
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

@jesper thanks for concrete explaination!

Does caching happen for other wrapper objects like Long, Double, Boolean, Short, Float, Byte?


i have same query as Anant.


Good luck!!
A small leak can sink a Gigantic ship.>
Nikhil Kumar C
Greenhorn

Joined: May 19, 2010
Posts: 2
Integer.valueOf(...) does not always return a new Integer object - instead, it manages a cache that contains Integer objects with all the values between -128 and 127.


Is there any specific reason why only integer values between -128 to 127 are being cached?
raul saini
Greenhorn

Joined: May 18, 2010
Posts: 18
Got it, Thank You Every One.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18500
    
  40

Nikhil Kumar C wrote:
Is there any specific reason why only integer values between -128 to 127 are being cached?


The specification defines the types and the ranges that must be cached. It does not define what should happen to the values of other types, or if they are outside of the required ranges.

The current Sun java implementation caches as required, plus... (1) it also caches long values, which is not required, and (2) there is a switch to allow the user to increase the range (caching values which are not required).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
To me it seems logical that the reasons the specification would call for cached
values are the usual - space and speed. On average, the code will run faster
and in less space with the cached primitives.

Jim ... ...


BEE MBA PMP SCJP-6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query: In boxing why == & != both works
 
Similar Threads
Wrapper
== and .equals clarification for Integer
Wrapper equality
Help me with this Wrapper class code