This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
(a+b == "ab")//prints nothing Because the == operator is not comparing the contents of the Strings instead it is comparing the address of the two Strings. Also String objects are immutable you can reassign references, but you can't change the contents of a String.
String c = a+b // Here you are creating a new String the new String will equal "ab" than == will print true; if (c == "ab")//print true But this is the wrong way of comparing String. Use c.equal("ab") instead dont forget String are Immutable.
1)dont use == to compare String instead use oneString.equals("anotherString").
2)a+b creates a temporary object and increases garbage collection than you should use StringBuffer to concat you string.
hope this help
[ April 25, 2007: Message edited by: Reda Mokrane ] [ April 25, 2007: Message edited by: Reda Mokrane ]
Yes, that behaviour is what you would expect. "zg" has the same contents as "z" + "g". That is what the equals() method tests for. Whenever you use an equals method, it is worth checking in that class' API documentation about equals. It should be shown as a method in its own right; if it appears in the box headed "inherited from Object" then it checks to see whether the two are the same object. See the equals method in the API for java.lang.Object.