Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

try this program

 
kumara swamy adurthi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Q7
{
public static void main(String[] args)
{
if( new Boolean("true") == new Boolean("true"))
System.out.println("True");
else
System.out.println("False");
}
}
i know this program but i donot know reason of this answer
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because each of your Boolean objects refers to a different address in memory.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Your testing if the two objects are equal, as in "sharing the same memory".

This might make it easier to understand:


/Svend Rost
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may also want to compare this with

Layne
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now try this program:

To understand the different result, look in the API for Boolean.valueOf(boolean). Boolean.valueOf(String) works the same way.

Bottom line: Only new always gives a new object. Only string literals, String constant expressions, and interned strings always give the same object for a given value. Other expressions offer no guarantee.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic