The equals method and = in Java

Source: Internet
Author: User

The equals method is a Java. Lang. Object Class method.

There are two usage instructions:

(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 to different objects,

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.

Variable generated by the String constant, where the memory address is equal,

So S3 = S4 is true (even if there is no value 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.
The differences between equals and = are as follows:

(1) for basic type comparison, you can only use = for comparison, but not equals.

For example:
Public class testequals {
Public static void main (string [] ARGs)
{
Int A = 3;
Int B = 4;
Int c = 3;
System. Out. println (A = B); // The result is false.
System. Out. println (A = C); // The result is true.
System. Out. println (A. Equals (c); // error. The compilation fails. The equals Method
// Comparison with basic types cannot be used
}
}
(2) For the packaging type of the basic type, such as Boolean, character, byte, shot, integer, long, float, double, and other reference variables, = is a comparison address, equals is compared. For example:
Public class testequals {
Public static void main (string [] ARGs)
{INTEGER n1 = new INTEGER (30 );
Integer n2 = new INTEGER (30 );
Integer N3 = new INTEGER (31 );
System. Out. println (n1 = n2); // The result is false. The two different integer objects have different addresses,
System. Out. println (n1 = N3); // the result of new INTEGER (30) or new INTEGER (31) is false.
System. out. println (n1.equals (N2); // The result is true. According to the instructions in the JDK documentation, the content in the object indicated by N1 and N2 is equal to 30, therefore, after equals is compared, the result is true.
System. Out. println (n1.equals (N3); // The result is false. Because the object content is different, one is 30 and the other is 31.
}
}
This is an integer instance. If it is another instance, such as double, character, and float, the same applies.

(3) Note: The following three classes are further described: String, stringbuffer (thread-safe variable character sequence), and stringbuilder (variable character sequence.
(A) First, introduce the usage of string. See the following example:
Public class testequals {
Public static void main (string [] ARGs ){
String S1 = "123 ";
String S2 = "123 ";
String S3 = "ABC ";
String S4 = new string ("123 ");
String S5 = new string ("123 ");
String S6 = new string ("ABC ");

System. Out. println (S1 = S2); // (1) True
System. Out. println (s1.equals (S2); // (2) True
System. Out. println (S1 = S3); // (3) flase
System. Out. println (s1.equals (S3); // (4) flase

System. Out. println (S4 = S5); // (5) flase
System. Out. println (s4.equals (S5); // (6) True
System. Out. println (S4 = s6); // (7) flase
System. Out. println (s4.equals (s6); // (8) flase

System. Out. println (S1 = S4); // (9) False
System. Out. println (s1.equals (S4); // (10) True
}
}
Explanation: S1 and S2 point to the object created by the String constant "123" respectively. In the constant pool, there is only one object with the content of 123. Two references S1 and S2 point to this object, therefore, the two referenced variables point to the same address, so the running result at (1) is true, because s1.equals (S2) is to compare S1 and S2 to the object content is equal, and we know that the content of both objects is a String constant "123", so Mark (2) the running result is true.
For analysis in the same way, S1 and S3 point to different objects and different content. Therefore, the running result at mark (3) and (4) is false.
Let's look at S4 and S5. The two referenced variables point to the same object (the content is 123), but these two objects use the new operator to create classes, two blocks of space are allocated to the two objects in the memory, so the memory addresses of these two objects are different. The story shows two different objects marked (5) s4 = S5 run result is false, but the content is the same, so s4.equals (S5) run result at mark (6) is true. Similarly, S4 and S6 point to different object addresses and different contents. Therefore, the running result at mark (7) (8) is false.
S1 and S4 point to two different objects respectively (because the addresses of these two objects in the memory are different, so the objects are different), so they are marked as (9) s1 = S4 running result is false, and s1.equals (S4) running result marked as (10) is true.
(4) let's look at another situation. Let's look at an example (this example is an example in chapter 3 of Java programming ideas ):
Class Value
{
Int I;
}
Public class extends smethod2 {
Public static void main (string [] ARGs ){
Value V1 = new value ();
Value v2 = new value ();
V1. I = v2. I = 100;
System. Out. println (v1.equals (V2); // (1) flase
System. Out. println (V1 = V2); // (2) True
}
}
Run result doubt: At first glance, the result is a little surprised. Why is it not true? Doesn't the equals method compare content?
Explanation: Yes. If the equals method is overwritten in the new class, it can be used to compare the content. However, in the above example, the class value does not overwrite the equals method in the object, but inherits the method, so it is used to compare the address, and V1 and V2 point to different objects, so the v1.equals (V2) running result at mark (1) is false, marked as (2) the run result of V1 = V2 is also false.

Summary: The introduction of equals and = is here. If you have better or newer explanations, please kindly advise. Thank you.

Related Article

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.