Identity and Equivalence in Java

Source: Internet
Author: User
Tags object object

Identity (Identity):
Java provides a contract operator: = = and!=, which can be used for basic numeric types, Boolean values, reference types. For example:
Two references Ref1 and Ref2 refer to the same object or null, the value of the expression Ref1==ref2 is true, even if the two references have different type declarations, the value of the expression is false.
The contract operator tests the identity of the reference, not the equivalence of the object. If two references point to the same object, they are the same. If two objects logically have the same value, Then they are equivalent. Equivalence is checked by the Equals method defined by object. For classes that are not the same thing as equality and identity, you should override the Equals method because:
Object.Equals assumes that an object is only equal to itself.
Attention:
Since the array does not define its own method, only inherits the object's methods, so its Equals method is always based on identity rather than equivalence, and the Equals method in the Java.util.Arrays class compares the array's equivalence by calculating the hash code based on the array contents.

Equivalence (equivalence):
Equals in object comparison:
Compare the memory locations of the recipient and Parameter objects, if the subclass does not provide a equals implementation, then use the equals implementation of the default comparison memory location. Java API documentation A section of the Equals method of object, which provides a list detailing the equality between two objects:
1. Reflexivity (reflexivity): X.equals (x)
2. Symmetry (symmetry): X.equals (y) if and only if Y.equals (x)
3. transitivity (transitivity): if X.equals (y) and Y.equals (z), then: X.equals (z)
4. Consistency (consistency): Given a consistent state, x.equals (y) returns a consistent result
5. Can be compared with null (comparison to NULL): X.equals (NULL)

Related applications:
1. A defensive clause that returns FALSE if the parameter is null
@override
public Boolean equals (Object object) {
if (object = = null) return false;
...
}
2.apple and Orange
If someone throws an "orange" into the method, immediately returns false defense (BG) clause, which ensures that the type of the argument matches the type of the recipient
@override
public Boolean equals (Object object) {
if (object = = null) return false;

if (This.getclass ()!=object.getclass ()) return false;
...
}

3. Why not use equals?
Class constants are unique: All class objects share an instance, because of the uniqueness of class constants, so you can use the!= operator to compare class constants without necessarily using method equals.

Of course, some people are more inclined to instanceof, if an object is an instance of the target class, or a subclass instance of the target class, then the operator instanceof returns TRUE. as follows:
public Boolean equals (Object object) {
if (object = = null) return false;

if (!) ( Object instanceof User) return false;
...
}
It is recommended to use method GetClass instead of instanceof, to compare the types of two classes, only two objects belonging to the same class, GetClass return: True, if you need to at the inheritance level regardless of the hierarchical position of the comparison object, then use the instanceof

4. String literal constants, equivalence and memory qualification
In general, use the contract operator: = = to compare strings to get the wrong result. However, for two identical string literal constants, they will point to the same string object, for example:
String str= "ABC";
if (str== "abc") {
....
}

Attention:
This can only be done if all of the string references involved are string literal constants, and if STR is changed to refer to a generated string object, then the = = the operator returns FALSE. To solve this problem, Memory Qualification (intern) can be performed on a string literal constant that is not exactly known to be referenced.

Intern Method:
The method returns a string object that has the same content as the string object to which the method is invoked, whereas any two identical strings are returned with the same string object after the Intern method is called. This mechanism allows us to compare string references to test their equivalence, replacing the execution of extremely slow testing of string content.
int putIn (String key) {
String u

<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.