Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boxing problems

 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • 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
 
Amit Wadhwaa
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Priya Viswam
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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.
 
j bande
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks Priya
i got it
 
ramesh kumar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic