File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boolean doubt 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 "Boolean doubt" Watch "Boolean doubt" New topic
Author

Boolean doubt

Kota li
Greenhorn

Joined: Sep 05, 2007
Posts: 20
public class Boxing6 {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
boolean b3 = true;
Boolean b4 = true;
System.out.println(b1==b2);
System.out.println(b1==b3);
System.out.println(b3 == b4);
System.out.println(b1 == b4);
}
}
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!

When you compare two distinct objects with == the result will be always false.

When you compare two primitives, it will be true when the value is the same or false if not.

When you compare an object of a wrapper class with a corresponding primitive, the object will be unboxed and the comparison will be as if both are primitives.

If the wrappers are of type Byte, Short, Character or Integer the story is a bit more complicated, but this is not part of the question.


Note that in the line with b4 a new object is created, just as in the first two lines, only with a different syntax (autoboxing).


Yours,
Bu.


all events occur in real time
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Burkhard Hassel:
... When you compare two distinct objects with == the result will be always false.

When you compare two primitives, it will be true when the value is the same or false if not...

The way I like to think of this is that == is a comparison of values. For primitives, this is straight forward. For objects, this means a comparison of references, so == will return true only of both operands reference the same object.

Hope that helps.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean doubt