What are the differences between ".equals()" and "==" (in Java)?
Both options are used to compare data.
Primitive types only accept "==": the equality operator, that compares if the primitive variables have the same value.
Reference variables accept both. "==" compares if both are references to the same area in the memory, which means that both variables references the same object. The method ".equals()" have the same meaning in some cases - the class Object and its subclasses that don't override the method ".equals()" (in any inheritance level).
As this method can be overriden, we could have 2 instances of the class A, where instance1== instance2 returns false, but instance1.equals( instance2) returns true.
Both options are used to compare data.
Primitive types only accept "==": the equality operator, that compares if the primitive variables have the same value.
Reference variables accept both. "==" compares if both are references to the same area in the memory, which means that both variables references the same object. The method ".equals()" have the same meaning in some cases - the class Object and its subclasses that don't override the method ".equals()" (in any inheritance level).
As this method can be overriden, we could have 2 instances of the class A, where instance1== instance2 returns false, but instance1.equals( instance2) returns true.
No comments:
Post a Comment
Please, before starting to comment, evaluate if what you want to write is useful, respectful and positive. A kid may read this also.
Give a good example!