posted 22 years ago
The equality operator, ==, compares reference values. The String literal, "String", refers to an instance of a String in the String constant pool. The String created by the String.trim method is not a String constant. When the references of the two Strings are compared they are found to be not equal.
If the two strings were compared using the String.equal method then the two Strings would be found to be equal because they contain the same characters.
If you were to invoke the String.intern method on the String returned by the String.trim method then the result of the intern method would be a reference to the String in the String constant pool. If the equality operator were applied to the String literal and the result of the intern method then the result would be true.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>