equals and = = in Java

Source: Internet
Author: User

the "= =" in Java is always compared to whether two objects are the same object (the reference points to the same piece of memory is the same object)

The use of equals () in Java must be hooked to the class and cannot be used alone. Some people understand that "= = compares objects, whereas the Equals () method compares content (that is, attributes inside objects)."

In fact, in Java, equals as a method, we cannot separate from the class to discuss it separately:

The Equals () method is derived from the object class, and the source code is as follows:

public boolean equals (Object obj)

{

return this = = obj;

}

The Equals method in the object class is identical to the = = function: Compares whether two objects are the same object. Simply, the string class as the object subclass overrides the Equals method of the object class in order to conform to its own characteristics, overriding the effect that if the value of two string objects is the same, the call to the Equals method returns True, otherwise false is returned.

  

The following demo code:

public class Test

{

public static void Main (string[] args)

{

String str1 = "Hello";

String str2 = new String ("Hello");

String str3 = new String ("Hello");

String STR4 = str3;//is the same object as Str3

System.out.println (str1 = = str2);//false, not the same object

System.out.println (str2 = = STR3);//false, not the same object

System.out.println (STR3 = = STR4);//true, is the same object

System.out.println (Str1.equals (str2));//true, Value (property) is the same

System.out.println (Str2.equals (STR3));//true, Value (property) is the same

A a1 = new A ();

A a2 = new A ();

SYSTEM.OUT.PRINTLN (a1 = = A2);//false, not the same object

System.out.println (A1.equals (A2))//false because the Equals method is not overridden, the Equals method of object is called directly (equal to = =), so the error (because it is not the same object)

}

}

Class A

{}

In Java, the implementation classes for all the base data types override the Equals method in the same way as the string class, which conforms to the "= = comparison object, equals comparison value" conclusion.

If a class is defined by you, it is the default comparison object if you do not override the Equals method. (Call methods directly in the object class)

Reference Link: http://jingyan.baidu.com/article/f96699bbc9d6ae894e3c1b81.html

equals and = = in Java

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.