File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes try  this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try  this program" Watch "try  this program" New topic

try this program

kumara swamy adurthi

Joined: Mar 16, 2005
Posts: 4
class Q7
public static void main(String[] args)
if( new Boolean("true") == new Boolean("true"))
i know this program but i donot know reason of this answer
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
It is because each of your Boolean objects refers to a different address in memory.
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904

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

Joined: Dec 06, 2001
Posts: 3061
You may also want to compare this with


Java API Documentation
The Java Tutorial
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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.

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: try this program
It's not a secret anymore!