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

Difference in result - Boolean

M Poo
Greenhorn

Joined: Jan 31, 2004
Posts: 6
Hello:
Can anyone please explain why the second line printed in the following program is "true" ? The first line prints "false" as expected since they are 2 distinct objects. I would have expected the same of the 2nd line too.
class TestBooleanValue {
public static void main (String[] args) {
System.out.println(new Boolean("false")==new Boolean("false"));
System.out.println(Boolean.valueOf("false")==Boolean.valueOf("false"));
}}

Output:
=======
false
true
TIA
MP
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi M,
Welcome to JavaRanch!
Can anyone please explain why the second line printed in the following program is "true" ? The first line prints "false" as expected since they are 2 distinct objects. I would have expected the same of the 2nd line too.

Because Boolean.valueOf("false") returns the static Boolean instance Boolean.FALSE, which of course == itself.
Best,
Phil.
M Poo
Greenhorn

Joined: Jan 31, 2004
Posts: 6
Thanks Phil. Now I get it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference in result - Boolean