= Differences from equal ()

Source: Internet
Author: User

"=" And "equals (object)" are often used to compare objects )". They are often confusing for beginners. The following is an example.
Public class example1
{
Public static void main (string [] ARGs)
{
String S1 = new string ("ABC ");
String S2 = new string ("ABC ");
// S1 = S2;
System. Out. println ("compare result with = ");
System. Out. println (S1 = S2); // false
}
}
Since the content of the two string objects is the same as "ABC", why do we use false first. That's because "=" compares the references of two objects, not their content. How can we compare the same content? The result of removing the comments of S1 = S2 is different because their references are the same.
We need to use the equals (object) method. Since the equals (object) method is the method defined in the object class, all classes defined by default are their subclasses. That is to say, the object class is the super class of all classes (super class, also called parent class, base class, etc.). The standard form of the equals (object) method in the object is
Public Boolean equals (Object OBJ)
The return type is Boolean, that is, true/false is the same as "=. The equals (object) method defined in the object class is two objects that are compared directly using "=". Therefore, the equals (object) in this case, equals (object) and "=" are compared references. Example ):
Public class example4
{
Public static void main (string [] ARGs)
{
Example4 E = new example4 ();
Example4 E4 = new example4 ();
System. Out. println ("compare results with equals (object ");
System. Out. println (E. Equals (E4); // The result is false.
System. Out. println ("compare result with = ");
System. Out. println (E = e4); // The result is false.
}
}
The special difference between the equals (object) method and "=" is that it can be overwritten, so we can overwrite it to compare data content rather than comparing references. Of course, JDK also has classes that overwrite the equals (object) method, such as Java. lang. string, which overwrites the equals (object) method inherited from the object to compare whether the content of the string is the same. Let's take a look at the example below:
Public class example1
{
Public static void main (string [] ARGs)
{
String S1 = new string ("ABC ");
String S2 = new string ("ABC ");
System. Out. println ("compare result with = ");
System. Out. println (S1 = S2); // false
System. Out. println ("compare results with equals (object ");
System. Out. println (s1.equals (S2); // true
}
}
In this example, equals (object) is compared to true. Use = to compare the result with false. The string. Equals (object) method directly compares the content of two strings. if the content is the same, true is returned. Otherwise, false is returned. You can change "ABC" to "ABCDE" to see if the result has changed.

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.