A couple things to remember when it comes to String objects. First of all, String objects are immutable, meaning that, once they've been created, they can't be changed. Therefore, this doesn't do what you'd expect it to:
You might have expected this to print out "str1str2", but you only get the first part of it...why?

Well, like I said, String objects are immutable. Therefore, you can't change s in any way. If you take a look at the
API Spec for String, you'll see this in the description of the concat method:
Concatenates the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.
Examples:
"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns "together"
Parameters:
str - the String that is concatenated to the end of this String.
Returns:
a string that represents the concatenation of this object's characters followed by the string argument's characters.
Notice that this method
returns the string after the concatention - it creates a new string to do that, it doesn't modify a String in any way. Therefore, in order to get that bit of code to do what you expected, you'd have to modify it like this:
Another thing about Strings is use of String literals. (If you do a search in this forum, you'll find gobs of information on this, so I'll try to sum up.)
When you have a line like this:
The JVM creates a new String object at runtime and inserts the value "some string" into it. However, if you have the line:
The work can be done at compile time. At that point, a reference is created in a constant table which references the String literal. This is sometimes called the String literal pool. Since these are two distinct ways of creating an object, it's only fitting that, if you were to use the == operator on them that the result would be false (the == operator only checks for reference equality) even though the result of .equals would be true.
Take a look at these threads for some more good info on String literals:
when will == return true for Strings Quick == question GC question One last note for you - garbage collection of Strings
is not on the exam.
I hope that helps,
Corey