The answer of your second question: If you use the == Operator, then you compare the references of an Object, not there value.
after " String ".trim() you got a new reference to a String Object, which contains the value "String".
To compare the value of 2 Strings, you have to use equals() or equalsIgnoreCase().
Joined: Mar 11, 2005
Let me take one addition.
Don't be confused by the result of the following code
If you try to compare Strings in this manner, you have to know, that there are not two strings createt. Java uses a Literal-Pool and uses for the property first and second the same String-Object. So the references are the same and the == Operator would return TRUE.
the trim() method will return a reference to a new object. In this case, it will refer to a newly created string with a value of "String".
in other words, you will end up with 3 String objects created. two in the string pool (with values of " String " and "String"), and a third created by the trim() call. since the == operator checks to see if we refer to the same object, we get a false.
if you had instead done
after being chastized for writing such an ugly test condition, you'd get a true (assuming i didn't make any typos).
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors