• 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

boxing problems

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

Boolean b = new Boolean(true);
Boolean c = new Boolean(true);
boolean d = true;

System.out.println(b == c);
System.out.println(b == d);

}

the code above gives the output as : false true

why is the answer false in the first print statement ?

the explanation that i have found out is as shown below :

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 (7f is 127 in decimal)

Short and Integer from -128 to 127

pl explain
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b and c are objects, when you use the == operator it will "return" true if both c and c reference to the same object.

/Svend Rost
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boolean b1=new Boolean(true);
Boolean b2=new Boolean(true);

System.out.println(b1==b2); //prints false

Boolean b3=true;
Boolean b4=true;

System.out.println(b3==b4); //prints true

I am not very sure why it happens, have to do some reading.. If somebody can explain that would be great
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sven,

In order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same:

Boolean


this is just a part of the explanation
but what it essentially means is that the two Boolean objects should be immutable since they have the same value.
if the two objects are immutable then they must have the same reference and the output should be true.

correct me

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


Boolean b1=new Boolean(true);
Boolean b2=new Boolean(true);



If you are using 'new' then separate objects will be created. That is the reason why you are getting false in the first case.

But if literals are used, then they will point to the same location.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Boxing{

Boolean b = new Boolean(true);
Boolean c = new Boolean(true);
boolean d = true;

System.out.println(b == c); //false because b and c are referring to two different objects.
System.out.println(b == d); // true because value of b and d are same. i mean both are true.

}
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Priya
i got it
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.println(b.hashCode()==c.hashCode());

if we are using new then new objects will be created but here why above code is returning true
 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

System.out.println(b.hashCode()==c.hashCode());

if we are using new then new objects will be created but here why above code is returning true



Wrapper classes overrides hashCode() and equals() function. Since both b and c are having the value true, they will both have the same hashCode value eventhough they point to two different objects.
reply
    Bookmark Topic Watch Topic
  • New Topic