The second statement is in the pool. So it prints true. You will not find these types of questions on the exam for sure ! You dont have to know the intricacies of such stuff. All you have to know is that...

* The out reference's println method in the System class will use the toString() method of an object when it is called.

* Strings have pools and will exhibit the behaviour shown in those two code fragments. [ April 24, 2006: Message edited by: John Meyers ]

If you replace: if(b1.toString()==(b1.toString())) by if(b1.toString().equals(b1.toString())) it will return true. So, I think you actually have two different Strings but with the same value. I hope I made some sens lol.

== when executed on references compares those references, which effectively are memory addresses. It doesn't compare the actual content of those memory addresses.

When you executed it on the results of 2 calls to toString() on the same reference, you were actually executing it on 2 different references.

You might as well have asked "if (1 == 2) { " for all the good that would do.

Indeed the ONLY way you could get a true result out of that when comparing object references is when comparing 2 identical string literals, as those could actually refer to the same reference in the string constant pool. But even then the result isn't guaranteed to yield true (though it in practice usually does). But even when executing 'if ("Bill Door" == ("Bill" + " Door")) { ' you're not guaranteed to get a true result.