comparison of values, that is, the comparison of memory addresses. In the preceding example, "the values of two variables are equal" is not printed because the two variables point to objects with different memory addresses.
Object Content Comparison
For reference variables, if we do not want to compare the memory addresses, we want to make a deeper comparison (such as object content. How to implement it? The Java object class provides the
are judged equal, no difference to basic variables, except for dynamic variables (that is, objects):
= =: reference equality (reference comparison). For object references, that is, the reference value is equal to the address. That is, if object a A, a==b indicates whether the addresses of a and b are equal.
Equals (): reference points to equal content (value comparison). Is the method of object, and its implementation is r
have a subclass that does not overwrite equals, the result of equals at this time is always false.That being the case, I would abandon inheritance and use compound (composition) instead.Using the above ColorPoint as an example, turn point into a colorpoint field instead of expanding. The code is as follows:public class ColorPoint { private final Point point; private final Color color; public Color
instance of an abstract class cannot be created.If the first operand of instanceof is null, the instanceof operator returns false, regardless of the type of the second operand.Tips for implementing the high-quality equals method:Use the = = operator to check if the parameter is a reference to this object.Use the instanceof operator to check if the parameter is the correct type.Convert the parameters to the correct type.For each critical field in the
1. Why rewrite the Clone () method?The shallow copy in Java does not re-create a new reference space for the object that you want to copy, and we'll rewrite the Clone () method when we need a deep copy.2. Example of the Equals () and Clone () method overloadsHourse class:Importjava.util.Date; Public classHouseImplementscloneable {Private intID; Private DoubleArea ; PrivateDate Whenbulit; House () {} house (
their storage location in the memory, because the equals method of the Object is compared with the binary equal sign (=), the comparison result is the same as that of the binary equal sign (=.
Equals () method in Object
public boolean equals(Object obj) { return (this == obj); }
Example:
Person. java
pack
The data types in Java can be divided into two categories:1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,booleanThe comparison between them, applying the double equals sign (= =), compares their values.2. Reference data type (Class)When they are compared with (= =), they compare the storage address in memory, so unless it is the same new object, their com
); }Comparative Substance:When comparing variables with "= =" in Java, the system uses the values stored in the "stack" of variables as the basis for comparison.The base data type is stored in the stack as its content value, and the object type is the address that is stored in the stack, which points to the object in the heap.The object class in the Java.lang package has the public boolean equals (Object
This is the desired result if any of the following conditions are met:Each instance of a class is inherently unique.Does not care if the class provides a "logical equality" test function.The superclass has overridden equals, and the behavior inherited from the superclass is also appropriate for subclasses.Class is private or package-level private, it can be determined that its Equals method is never called.
Equals ()Implement Equals ()Parameter must be an object, not a perimeter classWhen overriding Equals (), also overwrite the corresponding hashcode (), consistent with Equals ()Also note that the default value of string is nullpublic class Person {private String name;private
Always overwrite hashcode when Java overrides equals
Recent Learning Java Basics, encounter Java coverage equals always have to cover hashcode when there are many questions, and collea
=offset; - intj =Anotherstring.offset; - while(n--! = 0) { in if(v1[i++]! = v2[j++]) - return false ; to } + return true ; - } the } * return false; $}From the above you can see the case where the java.lang.String.equals (Object AnObject) method returns true:
Compare yourself with yourself;
Two objects are string and the contents are the same.
To be aware of:
are judged equal, no difference to basic variables, except for dynamic variables (that is, objects):
= =: values are equal. For object references, that is, the reference value is equal to the address. That is, if object a A, a==b indicates whether the addresses of a and b are equal.
Equals (): Is the method of object, and its implementation is return this = = Inputobject. However, this method is often overridden, such as String,in
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.