All objects are guaranteed to have a .equals()
method since Object contains a method, .equals()
, that returns a boolean. It is the subclass' job to override this method if a further defining definition is required. Without it (i.e. using ==
) only memory addresses are checked between two objects for equality. String overrides this .equals()
method and instead of using the memory address it returns the comparison of strings at the character level for equality.
A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder
classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same ==
would be a fine way to go. Strings themselves do not.