• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about == in wrappers

 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chetan Dodiya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i do some modification in this code like



then the output comes like
1 same
2 not same

why this tyoe of output
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why the range is -128 to 127 the declared variable are of Integer type...
please clear my doubt...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it may be creates separate objects on the heap.....
but not sure....
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saipavan vallabhaneni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Henry,
thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic