aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about == in wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about == in wrappers" Watch "question about == in wrappers" New topic
Author

question about == in wrappers

saipavan vallabhaneni
Ranch Hand

Joined: Nov 14, 2008
Posts: 34
output given in scjp 5(page no:236):
same objects

but when im running it on the system, the if statement is turning out to be false n im not getting any output

In the book its given that for Integers -128 to 127, the two wrappers will always be == to save memory...

but whether the values are in the range mentioned(-128 to 127) or not the == is always returning false...contrary to that said in the book...im confused...

the output im getting is differing from that given in scjp book
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
You seem to have variables i1, i2 in the equality test, whereas i3 and i4 are used to create the Integer objects. Is this just a typo?
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
The instances of Integer wrapper class in between -128 to 127 always be equal to save memory.......



here output is same
not same

[ November 14, 2008: Message edited by: Ganeshkumar cheekati ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
if i do some modification in this code like



then the output comes like
1 same
2 not same

why this tyoe of output


SCJP 1.5
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
i1,i2 values in between range i.e -128 to 127..

so output is same ok.

but the values of i3 and i4 are not in range so
if you use == check you will get ouput as notsame.

As per your code

you have used != for i3 and i4.

i3!=i4 ------i3 and i4 are different objects not same because not in range...

so i3!=i4 is true..

if(true)
System.out.println("not same");

output is:not same..

hope it clear...
Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
but why the range is -128 to 127 the declared variable are of Integer type...
please clear my doubt...
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
Boolean
Byte
Character from \u0000 to \u007f
Short and Integer from -128 to 127
saipavan vallabhaneni
Ranch Hand

Joined: Nov 14, 2008
Posts: 34
@ALL, thanks
thanks for your replies...
now i have one more


Output is :same
not same

but now if i say,



output is:not same
not same

y is it so???


what is the difference between assigning a integer literal and creating one using new keyword
Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
i think it may be creates separate objects on the heap.....
but not sure....
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362



whenever you are using new it will create new object so here 4 objects are created...
hence output is notsame and notsame.........
saipavan vallabhaneni
Ranch Hand

Joined: Nov 14, 2008
Posts: 34
thanks,
ya ..it creates objects..
but when we create wrappers using respective literal types then too objects are created(as wrappers are object representations or the primitive types)...are those objects same??? isn't that each wrapper a diff object???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18855
    
  40

Originally posted by saipavan vallabhaneni:
thanks,
ya ..it creates objects..
but when we create wrappers using respective literal types then too objects are created(as wrappers are object representations or the primitive types)...are those objects same??? isn't that each wrapper a diff object???


Autoboxing do *not* create objects directly. Boxing a primative goes through the valueOf() method. And from the JavaDoc of the valueOf() method...


public static Integer valueOf(int i)

Returns a 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.


The valueOf() method has a cache of Integer objects, and if the primative being boxed is in that range, the cached object is returned.

Henry


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

Joined: Nov 14, 2008
Posts: 34
@ Henry,
thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about == in wrappers