Java object comparison = and equals, javaequals

Source: Internet
Author: User

Java object comparison = and equals, javaequals

Reprinted: http://blog.csdn.net/bluesky_usc/article/details/51849125

1-Value Comparison

That is, if the content is the same, we think it is equal. For example: int I = 5; int j = 5; in this case, we say that I and j are equal, but they actually mean that I and j have the same content.

2. Comparison of reference types

However, in Java, besides the value type, there is also a reference type. The reference values of different objects are not equal, that is, they are in different address units in the memory. For example, we have defined the student class, which has two student object instances:

Student stu = new Student (); Student stu1 = new Student ();

In this case, either stu = stu1 or stu. the equals (stu1) method compares two objects and the results are not equal, because for the reference type, the default value is to compare the addresses referenced by the two objects, each object reference has its own unique address, so it is not equal.


Sometimes, when we compare two objects, if their content is the same, we think the two objects are equal, as shown in the above two student objects. What should we do now? In fact, it is very simple, as long as the equals () method is rewritten in the class, it takes a long time to carry out the content in the object. As shown above, we only need to rewrite the equals () method in the Student class.

Next, let's take a look at the instance! Comparison when the equals () method is not rewritten:

Student: Student

package com.bluesky;  2.  3.public class Student {  4.      5.    String name;  6.      7.    public Student(){  8.          9.    }  10.      11.    public Student(String name){  12.          13.        this.name=name;  14.          15.    } 16.     17.  18.}

 

Test:

 

 

1. package com. bluesky; 2. 3. public class Test {4. 5. public static void main (String [] args) {6. 7. int I = 5; 8. int j = 5; 9. 10. if (I = j) System. out. println ("I and j are equal! "); 11. else System. out. println (" not equal! "); 12. 13. student s = new Student ("BlueSky"); 14. student s1 = new Student ("BlueSky"); 15. 16. if (s = s1) System. out. println ("s and s1 are equal! "); 17. else System. out. println (" s and s1 are not equal! "); 18. 19. if (s. equals (s1) System. out. println (" s and s1 are equal! "); 20. else System. out. println (" s and s1 are not equal! "); 21.} 22 .}

 

Running result:

 

Rewrite the equals () method and then compare it again:

Student class:

Package com. bluesky; 1. 2. public class Student {3. 4. string name; 5. 6. public Student () {7. 8 .} 9. 10. public Student (String name) {11. 12. this. name = name; 13. 14 .} 15. 16. 17. public boolean equals (Object obj) {18. if (this = obj) // The input object is itself, such as s. equals (s); must be equal; 19. return true; 20. if (obj = null) // if the input object is null, it must be 21. return false; 22. if (getClass ()! = Obj. getClass () // if it is not of the same type, such as Studnet class and Animal class, 23. // you do not need to compare it. It is definitely not the same 24. return false; 25. student other = (Student) obj; 26. if (name = null) {27. if (other. name! = Null) 28. return false; 29.} else if (! Name. equals (other. name) // if the name attribute is equal, 30. return false; 31. return true; 32.} 33. 34. 35 .}

 

Test:

Package com. bluesky; 1. 2. public class Test {3. 4. public static void main (String [] args) {5. 6. int I = 5; 7. int j = 5; 8. 9. if (I = j) System. out. println ("I and j are equal! "); 10. else System. out. println (" not equal! "); 11. 12. student s = new Student ("BlueSky"); 13. student s1 = new Student ("BlueSky"); 14. 15. if (s = s1) System. out. println ("s and s1 are equal! "); 16. else System. out. println (" s and s1 are not equal! "); 17. 18. if (s. equals (s1) System. out. println (" s and s1 are equal! "); 19. else System. out. println (" s and s1 are not equal! "); 20.} 21 .}

Running result:

 

 

After the equals () method is rewritten, the s and s1 are equal. = You can only compare the addresses of the reference types, so they are not equal.

 

 

 

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.