! = Differences between null and equals

Source: Internet
Author: User
First, you need to first understand what their respective functions are: object. equals (null), the "equals ()" method is to compare whether "value" is equal; object = NULL, "=" comparison operator is used to compare whether "object" is the same. Java is an object-oriented programming language. If an object is new, it has a value. Its value can be "null "; if there is no new object at all, it will not exist. If there is no way to compare it, a null pointer exception will occur. The description is ambiguous. Here is an example: Object object1 = new object (); object object2 = new object (); object object3; object1.equals (object2 ); // This is true object1 = object2; // it may not be true object3 = object2; // an error is reported because object3 has not opened up memory, and object2 does not know who to point to for comparison. That is to say, each time a new object is a new memory, they stand on different locations for comparison, equals is to compare their values, and = is equivalent to comparing their locations!
 
Object = NULL determines whether the object is allocated memory space. If the object is not allocated memory space, that is, object = NULL, object. equals (null) will throw nullpointerexception. objects without memory space allocated cannot call any method. Also Talk About = and equals (). the former compares the memory addresses of two objects to determine whether the two objects are the same. The latter compares the two objects to determine whether the specific "values" are the same, for example, "AAA ". equals ("AAA") is true. In addition, if you want to implement the above functions of equals () for your own defined classes, you also need to override the equals () method of the object class. By default, it uses "=" for comparison, the above string class has already rewritten this method, so it can be used directly. Please forgive me for saying that "0 x-Dragon first 0" answered very well, but his sentence "object1.equals (object2); // This is true" is wrong, the result is false because the equals () method of the object uses "=" for comparison by default. object1 and object2 are obviously not the same object. Yes, but pay attention to the declared position: if it is declared as a class attribute, there is no problem. If it is declared in the method body, it must be assigned an initial value, even if it is null, for example, string a = NULL, otherwise it will not be initialized and cannot be called.

From: http://zhidao.baidu.com/question/80608165.html

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.