In general, the answer to your question is "yes", but...
.equals(...)will only compare what it is written to compare, no more, no less.- If a class does not override the equals method, then it defaults to the
equals(Object o)method of the closest parent class that has overridden this method. - If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the
Object#equals(Object o)method. Per the Object API this is the same as==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality. - Always remember to override
hashCodeif you overrideequalsso as not to "break the contract". As per the API, the result returned from thehashCode()method for two objects must be the same if theirequalsmethods show that they are equivalent. The converse is not necessarily true.
In general, the answer to your question is "yes", but...
.equals(...)will only compare what it is written to compare, no more, no less.- If a class does not override the equals method, then it defaults to the
equals(Object o)method of the closest parent class that has overridden this method. - If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the
Object#equals(Object o)method. Per the Object API this is the same as==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality. - Always remember to override
hashCodeif you overrideequalsso as not to "break the contract". As per the API, the result returned from thehashCode()method for two objects must be the same if theirequalsmethods show that they are equivalent. The converse is not necessarily true.
With respect to the String class:
The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not. If any two object references of type String refer to the same String instance then great! If the two object references refer to two different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.
On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".
As with any explanation, let it sink in.
I hope this clears things up a bit.
I'm currently working on a project that involves comparing strings, but I keep getting stuck on whether to use the "==" operator or the ".equals()" method. From what I've gathered so far, they seem to do the same thing - is it true? Or are there cases where one should be used over the other?
Videos
I'm on MOOC.fi and I'm a little confused on why you can't use .equals() when comparing two objects. And on part 5_12.Song programming exercise, I'm confused on why the parameter takes an Object parameter instead of a "Song" type, and then why do we have to typecast the object to compare it?
The question is just playing with you with confusing spacing.
b != b is the usual != (not equals) comparison.
On the other hand:
b =! b is better written as b = !b which is parsed as:
b = (!b)
Thus it's two operators.
- First invert
b. - Then assign it back to
b.
The assignment operator returns the assigned value. Therefore, (b =! b) evaluates to true - which is what you print out.
b != b means ! (b == b): the opposite of b == b.
b =! b is actually b = !b, an assignment. It's toggling b's value. An assignment evaluates to the value of the expression, so this will evaluate to !b (along with having changed the value of b).