• 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

How Does == Operator work in case Wrapper Classes?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I am prepparing for the SCJP 1.6 exam and was going through the mock exams. I got stuck with one question and unable to find the reason behind it.

class test {
public static void main(String[] args) {
test inst_test = new test();
int i1 = 2000;
int i2 = 2000;
int i3 = 2;
int i4 = 2;
Integer Ithree = new Integer(2); // 1
Integer Ifour = new Integer(2); // 2
System.out.println( Ithree == Ifour );
inst_test.method( i3 , i4 );
inst_test.method( i1 , i2 );
}
public void method( Integer i , Integer eye ) {
System.out.println(i == eye );
}
}

The Output of the above program is false, true, false. Why the last S.O.P is coming false. When I look at the reason it says 2000==2000 is false since it exceeds 127. But 2000 is declared as int. and If I change the value of i1 and i2 to 127 it returns true.

I think I have missed some thing during the preparation of SCJP 1.6. Please help me in understanding the above scenario.

Thanks in Advance.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Please tell us which exam this is from, otherwise you may not get an answer.

Rob Fred or Martijn may transfer this thread to the SCJP forum.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why does the first sop gives false? (i thought it won't and so i tried it myself; but to my disbelief , its false)
as i read somewhere,

In order to save memory, two instances of the following wrapper objects (created through boxing), will always be == when their primitive values are the same:
• Boolean
• Byte
• Character from \u0000 to \u007f
• Short and Integer from -128 to 127

clearly, the primitive values here are 2 and 2
so i'm confuzed about it!

also, the question raised by the creator of this post amazes me..

will wait for replies!
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Akshat,
This is a very interesting question.
The first output "false" makes sense because you have created two different integer objects using new operator and comparing the references which will be different.
The second print statment's output also makes sense since your comparing two integer primitives which are equal.
Now the third print statement's output is also correct because when you pass 2000 into the public void method( Integer i , Integer eye ) your wrapping that in a integer object but if the number you pass is between -128 to 127 then java would treat those wrapper as equal in order to save memory.You may ask y on the first print statement output is true this is becuase there you have manually created objects using new operator and in the third you passed it to the compile to wrap.
Hope this helps and good luck for your ex
 
Thapliyal Akshat
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nikhil,

For your response. Actually the first two responses are as per my expectations. But I am confused in third one. You said:

"When you pass 2000 into the public void method( Integer i , Integer eye ) you are wrapping that in a integer object but if the number you pass is between -128 to 127 then java would treat those wrapper as equal in order to save memory"?

How the memory is coming in this picture. Could you please elaborate you explanation? Why Integer class is behaving like a byte?


Ritchie,

I have already mentioned in my first line that I am preparing for SCJP 1.6 exam.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nikhil pasupukuntla wrote:
The first output "false" makes sense because you have created two different integer objects using new operator and comparing the references which will be different.



Correct.

nikhil pasupukuntla wrote:
The second print statment's output also makes sense since your comparing two integer primitives which are equal.



Actually, no. The primatives are being passed to a method -- which takes Integer objects. And it is the Integer objects which are being compared. Autoboxing is converting the int primatives to Integer objects.

The reason the two objects are equal is because autoboxing uses the valueOf() method to get the objects, which in turn, uses an integer cache. The number being boxed are within the range of the cache, and the same primitve gets the same object.

nikhil pasupukuntla wrote:
Now the third print statement's output is also correct because when you pass 2000 into the public void method( Integer i , Integer eye ) your wrapping that in a integer object but if the number you pass is between -128 to 127 then java would treat those wrapper as equal in order to save memory.You may ask y on the first print statement output is true this is becuase there you have manually created objects using new operator and in the third you passed it to the compile to wrap.



As for the third statement, they are not equal because the value being boxed in not in the range of the integer cache -- hence, they get newly instantiated objects.

Henry

 
Nikhil Pasupukuntla
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Akshat!
See when the number is between -128 to 127 the compiler instead of creating two different integer objects treats them as equal and creates only one object (this way its saving memory right).
 
Deepak Giri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks henry.. for the "integer cache" concept. now it really makes sense to me

 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thapliyal Akshat wrote:
I have already mentioned in my first line that I am preparing for SCJP 1.6 exam.



Yes, but we would like to know which mock exam this question came from.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a small note, the cache is with in a static class which will not be instantiate until the first time it is needed, therefore if valueOf() method is never invoked, the cache will never create. And on the other hand, the first time it’s invoked, it’s has to create 256 objects. (Henry, Please correct me if i am wrong).
 
Thapliyal Akshat
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I am able to understand what's going on.

Thanks for everyone for clearing the muddle of clouds and enhancing my knowledge.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic