Deep Java in the Equals method, how to compare the contents of two objects is equal? What does the object's content refer to?

Source: Internet
Author: User

Check if objects are equal
relational operators = = and!= are also applicable to all objects, but their meaning usually makes it less likely for people in the first Java domain to find north. Here is an example:

: Equivalence.java

public class Equivalence {
public static void Main (string[] args) {
Integer n1 = new Integer (47);
Integer n2 = new Integer (47);
SYSTEM.OUT.PRINTLN (n1 = = N2);
System.out.println (N1!= n2);
}
} ///:~

The expression System.out.println (n1 = = n2) prints out the internal Boolean comparison results. The average person would think that the output must be true first, then false, since two integer objects are the same. However, although the contents of the object are the same, the handle (which can be understood as the address in memory of two objects) is different, while = = and!= are just the object handles. So the output is actually first false and then true. This naturally surprises the first person in contact.
If you want to compare the actual content of two objects is the same, how to operate it. At this point, you must use the Special method equals () that all objects apply to. But this method does not apply to "main type", those types directly use = = and!= can be. The following examples illustrate how to use:


: Equalsmethod.java

public class Equalsmethod {
public static void Main (string[] args) {
Integer n1 = new Integer (47);
Integer n2 = new Integer (47);
System.out.println (N1.equals (N2));
}
} ///:~

As we expected, the result at this point is true. But that's not the end of it. Let's say you created your own class, like this:

: Equalsmethod2.java

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));
}
} ///:~

The result is changed back to false at this time. This is because the default behavior of equals () is the comparison handle. So unless you change equals () in your new class, it's impossible to show the behavior we want. But pay attention to this behavior of equals () and perhaps avoid some "catastrophic" events.

For the object class, it provides a most rigorous implementation, that is, only the same object is, the Equals method returns True, which is often referred to as a reference comparison rather than a value comparison. This implementation is so tight that it has no practical meaning, so in a specific subclass (relative to object), if we want to compare the value of an object, we must implement our own Equals method.
Most Java class libraries implement Equals (), so it actually compares the contents of objects, not their handles.

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.