Java J2SE/base class questions about why you should rewrite the Equals method for the object class in Java __java

Source: Internet
Author: User
Tags garbage collection stringbuffer

http://community.csdn.net/Expert/topic/5732/5732870.xml?temp=6.341189E-02

My book says the Equals method in object is used to detect whether an object is equal to another object, and the method of detection is to determine whether two objects have the same reference, and if they have the same reference, they must be equal. But for most classes this judgment has little meaning, and I want to know why this judgment is meaningless for most classes. The book does not give an explanation, I would like to ask a master, preferably in detail.

Answer 1:

For a simple example:
A user logs on to a website, enters a username and password, and then submits the server to generate an object User1,
The server extracts user information from the database and also generates an object User2,user1 and User2 with the same username and password. If you do not modify the Equals () method, two objects are necessarily different (they are equal if they have the same reference), and all the Equals () methods are modified:

public boolean equals (Object ob) {
User u= (user) OB;
if (U.username.equals (this.username) &&u.password.equals (This.password)) return true;
else return false;
}

Modifying the Equals () method is related to the actual application.

Answer 2:

String S1 = "ASDF";
String s2 = "ASDF";
S1 = = S2 returns false, but s1.equals (S2) returns true.
====================================
This example is a bit of a problem, hehe.
Answer the question first:
Object in the Equals method when A.equals (b), if a,b is pointing to the same object returns true otherwise returns false, that is, it compares the memory address, see two references are not the same memory address:
Student s1=new Student ();
Student s2=s1;
S1.equals (S2);//return True
But our business is often compared to two references are equal, not to see their address is the same, if you want to compare the address can be compared with s1==s2, then we usually want to know two different address objects, their content is the same, but what is called the same content. This needs to be defined according to the different business, the object class can not know what is called the same, for example, we may think that the two students are the same student number is the same students, then the Equals () method to compare the number on the line, say a more extreme example, For example, I can definitely define any condition as equal, as long as the business needs, I can define 85==90 as true, because I think as long as accurate to 10-bit equality is considered equal.

In addition to the general rule: when we write the Equals method, we should pay attention to meet three principles:
1. Self-Student: s=new Student (); S.equals (s); be sure to return true;
2, Reflexivity: s1.equals (S2) is true to ensure that s2.equals (S1) is also true;
3, transitivity: s1.equals (S2) is true and S1.equals (S3) is true, S1.equals (S3) is true;
Find an integrated development environment to automatically generate a Equals method, study the generated code will find some of the details.

Again, String class, first of all, the string is a very high rate of use in the program class, in order to improve efficiency, the string is managed with a "pool."
String s1= "Asdf", which is not in the heap, but in a string pool, and when a variable is created sting s2= "ASDF", the pool is searched first, and if this string exists, it is no longer new, but the S2 also points to that string, So S1 and S2 are pointing to the same object, so S1==s2 is true; but string s3=new string ("asdf"); it's different, this is forcing an object to be created in the heap space and not pointing to a string pool, so the S1==S3 is false; But S1.equals (S3); is true; String S4=new string ("asdf"); is also creating an object in the heap space, and S3==S4 is False
Again, string is designed to be immutable, and you may find that S1 and S2 point to the same object, and when I do the S1 operation, S2 does not change. This is obviously going to bring strange bugs to the program, but a string is immutable, and when assigning a new value to s1= "XYZ", instead of changing "asdf" to "XYZ", instead of creating a string "xyz", let S1 point to it, S2 or point "asdf" will not be affected.
However, when a string needs to change frequently, it creates a large number of useless objects that may persist for a long time until garbage collection. To avoid this problem, when we change a string frequently, we should use the StringBuffer class instead of the string class, and then call StringBuffer's ToString () method to get the last string object when the operation completes


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.