Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference in result - Boolean

 
M Poo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Phil. Now I get it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic