Java equals method and = usage, javaequals

Source: Internet
Author: User

Java equals method and = usage, javaequals

Equals method usage and = usage in java (refer to 1)
The equals method is a java. lang. Object Class method.
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 // false the memory addresses of the two variables are different, that is, they point to different objects,
The content of variables s1.equals (s2) // true is abc, so they are equal.

Note (1:
If:

StringBuffer s1 = new StringBuffer("a");StringBuffer s2 = new StringBuffer("a");  

Result:

S1.equals (s2) // false

Explanation:

The equals method is not redefined in the StringBuffer class, so this method comes from the Object class, And the equals method in the Object class is used to compare the "Address", so it is equal to false.

Note (2:

Note that s3 and s4 are different from each other. Because s3 and s4 are two characters, the variable generated by the String constant contains the same memory address, 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 the first address of the object in the heap memory, it is used to compare whether two referenced variables point to the same object.
For example:

Class A {A obj1 = new A (); A obj2 = new A () ;}// then: obj1 = obj2 is false obj1.equals (obj2) is false // but with the following sentence added: obj1 = obj2; // If obj1 = obj2 is true, obj1.equals (obj2) is true.

In short:

The equals method compares the content of a string, but does not compare whether the object to which it points is the same for a non-string. = 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 (. equals (c); // error, compilation failed, equals method // cannot be used to compare with basic types }}

(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. Two different Integer objects have different addresses. 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 Syst. Em. 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, the same applies to other types, such as Double, Character, and Float.

(3) Note: This class includes String, StringBuffer (thread-safe variable character sequence), and StringBuilder (variable character sequence.
(A first introduces 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);//(1true    System.out.println(s1.equals(s2));//(2true    System.out.println(s1 == s3);//(3flase    System.out.println(s1.equals(s3));//(4flase    System.out.println(s4 == s5);//(5flase    System.out.println(s4.equals(s5));//(6true    System.out.println(s4 == s6);//(7flase    System.out.println(s4.equals(s6));//(8flase    System.out.println(s1 == s4);//(9false    System.out.println(s1.equals(s4));//(10true  }} 

Answer:

S1 and s2 respectively point to the object created by the String constant "123". 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. Therefore, (the running result at 1 is true, because s1.equals (s2) it is to compare whether the content of the object pointed to by s1 and s2 is equal, and we know that the content of both objects is a String constant "123", so the mark (the running result at 2 is true. For analysis in the same way, s1 and s3 point to different objects and different content, so Mark (3 and (4 run the result is false. Let's look at s4 and s5. The two referenced variables point to the same object (the content is 123, but the 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. There are two different objects in the story, mark (s4 = s5 running result of 5 is false, but the content is the same, so Mark (s4.equals (s5) running result of 6 is true. Similarly, s4 and s6 point to different object addresses and different contents. So Mark (7 (the running result at 8 is false. S1 and s4 point to two different objects respectively (the reason is that these two objects have different addresses in the memory, so the objects are different, therefore, the s1.equals (s4) running result marked as (s1 = s4 at 9 is false, and the s1.equals (s4) running result marked as true.

(4) let's look at another situation. Let's look at an example (this example is an example of the first chapter of Java programming ideas:

class Value {
    int i;
}

public class EqualsMethod2 {
    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));// (1flase
        System.out.println(v1 == v2);// (2false
    }
}

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 preceding example, the class Value does not overwrite the equals method in the Object, but inherits the method. Therefore, it is used to compare the address, and v1 and v2 point to different objects, therefore, the run result of v1.equals (v2) marked at 1 is false, and the run result marked at v1 = v2 is false.

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.