Jaime Lozano wrote: I don't understand why the line of code 28 works when I think it shouldn't as it is String comparison whith == instead of equals().
Here only two String objects are created by String literals "red" and "yellow" having respective values. The references of these two Strings are stored in String constant pool. Means all three String literals "red" refer to the same object whose reference was stored in String constant pool so when you use == It compares references since all String literal "red" refer to the same String object so It returns true.
If you make some changes as below It will not return true unless you use equals() method of String because
str referring String object which is created using new operator so It doesn't matter although you have reference of a String having same content in String constant pool It will create new String, where
str referring String object and String object created by String literal "red" will have different references.
To know more about String literals please go through this -->
Strings, Literally worth reading.