Notes | Objects in this article I will introduce some Java problems with the basic processing patterns of objects.
First Look at an example of object equality equivalence:
First look at the following code:
public class equaltest1{
public static void Main (string[] args) {
integer n1 = new Integer (20);
integer n2 = new Integer (20);
System.out.println (n1 = = N2);
System.out.println (N1!= n2);
}
}
The purpose of the
program is to output the comparison results in parentheses (Boolean values), and it is easy for the first-time Java-exposed person to assume that the output is true and then false.
but actually the result is false and then true, because although the values of the two integer objects are the same, their reference are different. (Note: The meaning of reference has been introduced in my last study note, and it is not discussed here.) )
to explain the above problem, we should understand Java's basic processing mode for objects:
when you're manipulating an object, you're actually manipulating its reference, such as a = B, which points A and B to the object that the original B points to, and if you change the content of a, you change the content of B! Because A and B contain the same object reference.
the reference stored in the original A is overwritten in the process of assignment, and is actually lost because the garbage collector (garbage collector) clears the object that the reference originally pointed to at the appropriate time.
So how do you know if the object's content is equal? Here you will use Equals (), see the following code:
public class equaltest2{
public static void Main (string[] args) {
integer n1 = new Integer (20);
integer n2 = new Integer (20);
System.out.println (N1.equals (N2));
}
}
This output is the true of what we expect from the door. Then, things are not so simple, if you build your own class, then what will happen? Please teller the surface code:
class value{
int i;
}
public class equaltest3{
public static void Main (string[] args) {
Value V1 = new value ();
Value v2 = new value ();
v1.i = v2.i = 20;
System.out.println (V1.equals (v2));
}
}
result output False again, this is why??
in fact, the default behavior of equals () is to compare with reference, so unless you overwrite (override) equals () in your class, you won't get the expected result. Most classes in the Java Standard library override Equals (), so they all compare the object's content to the same, so that the problem is not difficult to solve.
related overwrite (override) technology will be introduced in the next in-depth article:)
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