aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes boxing problems 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 "boxing problems" Watch "boxing problems" New topic
Author

boxing problems

Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
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

Joined: Oct 23, 2002
Posts: 904
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

Joined: Feb 15, 2007
Posts: 74
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


SCJP 5 94%<br /><a href="http://amit-wadhwa.blogspot.com/" target="_blank" rel="nofollow">My Blog</a>
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
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

Joined: Dec 28, 2006
Posts: 81

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.


SCJP 1.5<br />SCWCD 1.4
j bande
Greenhorn

Joined: Mar 08, 2007
Posts: 24
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

Joined: Feb 18, 2007
Posts: 114
thanks Priya
i got it
ramesh kumar
Greenhorn

Joined: Aug 15, 2006
Posts: 25
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

Joined: Dec 28, 2006
Posts: 81
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: boxing problems
 
Similar Threads
From Majji's Exam
Boolean values
Explain please
Wrapper Classes ,== and !=
Wrapper Classes Autoboxing Concept