The difference between = = in Java and equals ()

Source: Internet
Author: User
Tags true true

Declaration Reprint Source: http://blog.csdn.net/striverli/article/details/52997927

the = = and Equals () methods are methods of comparing equality, what are the differences and connections?
First, the = = number compares the value when comparing the base data type, and compares the address values of two objects when comparing two objects with the = = Number:

int x = 10;int y = 10;String str1 = new String("abc");String str2 = new String("abc");System.out.println(x == y); // 输出trueSystem.out.println(str1 == str2); // 输出false
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

What about the Equals () method? We can see by looking at the source that the Equals () method exists in the object class because the object class is the direct or indirect parent class of all classes, that is, the Equals () method in all classes inherits from the object class, and through the source we find that The Equals () method in the object class relies on the = = number, so in all classes that do not override the Equals () method, calling the Equals () method is the same as comparing the value of the address with the = = sign, however, in all classes provided by Java, Most classes override the Equals () method, and the overridden Equals () method generally compares the values of two objects:

Here I define a student class, without overriding the Equals () method, and the final output is: false


After I rewrite the Equals () method, the output turns true.

Now that some of the basics have been talked about, let's go back to the first example:

String str1 = new String("abc");String str2 = new String("abc");System.out.println(s1.equals(s2));System.out.println(s1 == s2);
    • 1
    • 2
    • 3
    • 4

According to the above, the first one is true, the second is false, and it is true, so continue to look at the following example:

"abc";String s2 = "abc";System.out.println(s1.equals(s2));System.out.println(s1 == s2);
    • 1
    • 2
    • 3
    • 4

Is the result the same as the previous one? The answer is: true True
Why is the second one true?
this involves a constant pool in memory , a constant pool is part of the method area, and when run to S1 create an object, if not in the constant pool, create an object "ABC" in the Constant pool, and use it directly when creating the second time, so the object created two times is actually the same object. Their address values are equal.

The one in the previous example

String str1 = new String("abc");
    • 1

What's going on here?
In fact, the object is created two times , in order to create the object "ABC" in the constant pool , once the object str1 is created in the heap memory . Therefore, the address values of str1 and str2 are not equal.

  

The difference between = = in Java and equals ()

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.