Differences between "=", "equals", and "instanceof" Java basics"

Source: Internet
Author: User

(1) For string variables, the comparison method is different when "=" and "equals ()" are used to compare strings.

"=" Compares the values of the two variables, that is, the first address of the two objects in the memory.

"Equals ()" compares whether the content in the string is the same.

For example:

String S1, S2, S3 = "ABC", S4 = "ABC ";

S1 = new string ("ABC ");

S2 = new string ("ABC ");

So:

S1 = S2 is false // The memory addresses of the two variables are different, that is, they point toLike not,

Therefore, they are not equal.

S1.equals (S2) is true // the content of the two variables is ABC, so they are equal.

 

Note (1 ):

If: stringbuffer S1 = new stringbuffer ("");
Stringbuffer S2 = new stringbuffer ("");

Result: s1.equals (S2) // false

Explanation: The stringbuffer class does not redefine the equals method. Therefore, this method comes from the object class, The equals method in the object class is used to compare "addresses", so it is equal to false.

Note (2 ):

For S3 and S4, note that S3 and S4 are two characters different.

StringThe variable generated by the constant, where the memory address is equal,

So S3 = S4 is true (even if noThere is an assignment statement such as S3 = S4)

(2) For non-string variables, the "=" and "equals" methods are used to compare their functions.

The first address of the object in heap memory, which is used to compare whether two referenced variables point to the same object.

For example:

Class

{

A obj1 = new ();

A obj2 = new ();

}

So: obj1 = obj2 is false

Obj1.equals (obj2) is false

 

But Add the following sentence: obj1 = obj2;

So obj1 = obj2 is true

Obj1.equals (obj2) is true

 

In short, the equals method is compared for strings, but not strings.

Whether the object to which it points is the same.

= The comparison operator is also used to compare whether the object is the same, that is, the first address of the object in the memory.

 

The equals method is redefined in the string class, and the value is compared, not the address. So it is true.

 

By the way, we will summarize the differences between the equals method and instanceof:

The instanceof operator is used to determine whether an object referenced by a reference type is an instance of a class.
For reference type variables, the Java compiler only compiles the variables according to the class declared by Mr.
The Type explicitly declared by the operating element on the left of instanceof must be of the same type or have an inheritance relationship with the operation element on the right, that is, it is located on the same inheritance branch of the inheritance tree; otherwise, the compilation fails.
The comparison rule of the equals () method of the object class is: if the object referenced by the OBJ parameter is the same as the current object, true is returned; otherwise, false is returned.
Some Classes in JDK overwrite the equals () method of the object class. Their Comparison rules are as follows:
If the two objects have the same type and the content is the same, true is returned.
These classes include: Java. Io. File, java. util. Date, java. Lang. string, and packaging class (such as Java. Lang. Integer and Java. Lang. Double ).
If you define a class, you can repeat the equals () method and define the comparison rules.

The requirements for equals () in Java are as follows, which must be followed. The following points are also different from those of instance:
• Symmetry: If X. Equals (y) returns "true", then Y. Equals (x) returns "true ".
• Reflex: X. Equals (x) must return "true ".
• Analogy: If X. equals (y) returns "true", and Y. equals (z) returns "true", then z. equals (x) should also return "true ".
• Consistency: If X. equals (y) returns "true", as long as the content of X and Y remains unchanged, No matter you repeat X. "True" is returned for the number of equals (y) times ".
• In any case, X. Equals (null) always returns "false"; X. Equals (and X objects of different types) always returns "false ".

 

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.