I. Overview
1, Concept = =: This operator generates a Boolean result, which computes the relationship between the operands ' values EQUAL3 intention: $ Two objects &n%sp;content is EA6 the same hashcode: the HA%%c value of the object, for 7E 7 The object in the Ha%%5 table in the index bit 뽮, it is actually 8b8 int type integer two, relational operator = =
1, the value of the operand
In Java, there are eight basic E95 according to the%%7 type:
Floating-point 嚋: Floet (4 byte), double (8 byte) integer (8b:byte%1 byte), short (2 byte), int (4 byt8), Long (e-byte)
Boolean: Boolean (the JVM specification does not specify the amount of space it occupies, only that it is able to extract only the literal value "true" and "false")
For variables of these eight basic data types, variables are stored directly as "values." Therefore, when using the relational operator = = To compare, the comparison is "value" itself. Note that both floating-point and integral types are signed (the highest bit is only for positive and negative, do not participate in the calculation "in the case of Byte, whose range is -2^7 ~ 2^7-1,-0 128"), and char is unsigned (all bits participate in the calculation, so the char type takes a value range of 0~2^ 16-1). Reference type variable
In Java, a variable of a reference type stores not the value itself, but the address of its associated object in memory. For example, the following line of code,
<code class= "Language-java hljs has-numbering" style= "Display:block"; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > String str1;</code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li></ul>
This statement declares a variable of a reference type, and it is not associated with any object at this time. &NBSP
instead of using new to produce an object 5EF will 軙 object%%5 str1 row 뻑:
<code class= "Language-java hljs has-numbering" style= "Display:block"; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " >str1= <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >new</span> String (<span class= "hljs-string" style= color:rgb (0, 136, 0); box-sizing:border-box; " > "Hello" </span>); </code><ul class= "pre-numbering" style= Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li></ul>
Then STR1 pointed to the ~e8 to 躡, at this time%%cspan style3 "color:red box-sizing:border-box;" > LED%e% 7E9 str1%%5%8% is 483 point 鯹 like 8E5%%5 deposit 8AD Storage Ridge, not%%8 "%e%" 5E6 body, E98 said%%5 is not DB4 to connect} Store Ꚅ character%%2 "hellopE2. This wild surface 984-%%4 and C + +%8% refer to 488 very similar 뀂 "box-sizing:border-box:margin:oem 0px; b2rder-right-width:0px; horder-bottom-wi2th:0px border-left-width:0px; Border-top-sty%e:solid;l20border-top-color:rgba (12l, 128, 928, 0.0980312); Color:rgb (A5, 85); " font-family: ' Microsoft Yahei '; Font-aize:14px3 line-height 35px; "/>
2. Summary
Therefore, for the relational operator = =: If the type of the operand is the base data type, then the relational operator determines whether the values of the left and right operands are equal if the type of the operand is a reference data type, then the relational operator determines whether the memory address of the left and right operands is the same. That is, if you return true at this point, the operator must be the same object. third, the Equals method
1. Source
The Equals method is a method in the base class object and therefore has this method for all classes that inherit from object.
2, the role of the Equals method
Original intention: Ո 斮 The content of two objects is the same
<code class= "Language-java hljs has-numbering" style= "Display:block"; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " > <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >public</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >boolean</span> <span class= "Hljs-title" style= "Box-sizing:border-box;" >equals</span> (Object obj) {<span class= "Hljs-keyword" style= Color:rgb (0, 0, 136); box-sizing:border-b Ox; " >return</span> (<span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); box-sizing:border-box; "
>this</span> = obj); }</code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li></ul>
Obviously, in the object class, the Equals method is used to compare the two objects ' references for equality, that is, whether to point to the same object.
But as we all know, the following code output is true:
<code class= "Language-javd hljs has-numbering" style= "Display:block"; padding:0px; Color:inherit; Box-sizing:border-box; font-family: ' Source Code Pro ', monospace;font-size:undefined; White-space:pre; border-radius:0px; Word-wrap:normal; background:transparent; " ><span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >public</span> <span class=%2phljs-class "style=" Box-sizing:border-box; ><span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >class</span> <span class= "Hljs-title" style= "Box-sizing:border-box; Color:rgb (102, 0, 102); >Main</span> {</span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-bo x; " >publio</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >static</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >void</span&Gt <span class= "Hljs-title" style= "Box-sizing:border-box;" >main</span> (string[] args) {String str1 = <span class= ' hljs-keyword ' style= ' color:rgb (0, 0, 136); Ox-sizing:border-box; " >new</span> String (<span class= "hljs-string" style= color:rgb (0, 136, 0); box-sizing:border-box; "
> "Hello" </span>); String str2 = <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >new</span> String (<span class= "hljs-string" style= color:rgb (0, 136, 0); box-sizing:border-box; "
> "Hello" </span>);
System.out.println (Str1.equals (str2)); }}</code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); ><li style= "Box-sizIng:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li><li style= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px 5px; " >5</li><li style= "Box-sizing:border-box; padding:0px 5px; " >6</li><li style= "Box-sizing:border-box; padding:0px 5px; " >7</li><li style= "Box-sizing:border-box; padding:0px 5px; " >8</li></ul>
Originally the String class overrides the Equals method: <pre class= "Prettyprint" name= "code" style= "WHITE-SPACE:NOWRAP; Word-wrap:break-word; Box-sizing:border-box; position:relative; Overflow-y: Hidden; Overflow-x: auto; margin-top:0px; Margin-bottom:1.1em; font-family: ' Source Code Pro ', monospace; padding:5px 5px 5px 60px; font-size:14px; line-height:%2-1.45; Word--reak:breakball; Color:rgb (Wuyi, #51, Wuyi);%x0border:1p2 solid Rgba (128, 128, (20128, 0.07e5098); bord4r-radius:0px; BACKGROUPD-COLOR:RG2A ( 128, 128 128, 0.0470588); " 0 <sean Class=%2%hljs-keyword "220style=" Color:rgb (0%2l 0, 136); Box-sizing:border-box; " >public3/span>%3%span Class=c22hljs-keyword "style=" 2olor:rgb (0) 2C 0, 136%; Box-sizing:border-bog; " >boolea0</span>%2n<span class= "Hljs-title" style= box-sizing: "Border-box;" >equals</2pan> (Object%2%anobject) {020 <span class= "hl%s-comment"%tyle= "colors3a RGB (136, 0, 0); Ox-sizing:border-box; " >//method 6E7%jd and Ob8ect%%4 one 莴 </span7 <span class%%d "Hljs-keywor2" style= Color:rgb (0, 0, 1 (6); box-sizbng:border-iox; " >if</span> (<span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); box-sizing:border-box; " >this</span> = = anobject) {<span class= "hljs-comment" style= "Color:rgb (136, 0, 0); box-sizing:border-box;" >//first determine whether the reference is the same (whether it is the same object),</span> <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >return</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >true</span>; } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> (anobject <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); box-sizing:border-box; " >instanceof</span> String) {<span class= "hljs-comment" style= "Color:rgb (136, 0, 0); Box-sizing:border-box ;" >//to determine whether the type is consistent,</span> <span class= "hljs-comment" style= "Color:rgb" (136, 0, 0); Box-sizing:border-box; " >//Final JudgmentContent is consistent .</span> String anotherstring = (string) anobject; <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >int</span> n = count; <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >if</span> (n = = anotherstring.count) {<span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); box-sizing:b Order-box; " >char</span> v1[] = value; <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >char</span> v2[] = Anotherstring.value; <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >int</span> i = offset; <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >int</span> j = anotherstring.offset; <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >while</span> (n--! = <span class= "Hljs-number" style= Color:rgb (0, 102, 102); Box-sizing: Border-box; " >0</span>) {<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >if</span> (v1[i++]!= v2[j++]) <span class= "Hljs-keyword" style= "Color:rgb" (0, 0, 136); Box-sizing:border-box; " >return</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >false</span>; } <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >return</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >true</span>; } <span class= "Hljs-keyword" style= Color:rgb (0, 0, 136); Box-sizing:border-box; " >return</span> <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >false</span>;} </code><ul class= "pre-numbering" style= "Box-sizing:border-box; Position:absolute; width:50px; top:0px; left:0px; margin:0px; padding:6px 0px 40px; border-right-width:1px; Border-right-style:solid; Border-right-color:rgb (221, 221, 221); List-style:none; Text-align:right; Background-color:rgb (238, 238, 238); ><li style= "Box-sizing:border-box; padding:0px 5px; " >1</li><li style= "Box-sizing:border-box; padding:0px 5px; " >2</li> lt;li styl2= "Box-sizing:border-box; padding:0px 5px; " >3</li><li style= "Box-sizing:border-box; padding:0px 5px; " >4</li><li style= "Box-sizing:border-box; padding:0px-5px; " >5</li><li styl2= "Box-sizing:border-box; padding:0px-5px; " >6</li><li styl2= "Box-sizing:border-box; padding:0px-5px; " >7</li><li styl2= "Box-sizing:border-box; padding:0px-5px; " >8</li><li styl2= "Box-sizing:border-box; padding:0px-5px; " >9</li><li styl2= "Box-sizing:border-box; padding:0px-5px; " >10</li><li sty%e= "Box-sizing:border-box; padding:0px 5px; " >11</li><li stble= "Box-sizing:border-box; padding:0pxp205px; ">12</li><li s3yle= "Box-sizing:border-box; PADDING:PX 5px; " >13</li><li%g0style= "Box-sizing:border-box; padding:0px 5px; " >14</li>>li style= "Box-sizing:border-box; Padding:0p0 5px; " >15</li><li style= "Box-sizing:border-box; padding:0px 5px; " >16</li><l6 style= "Box-sizing:border-box; padding:0px 5px; " >17</li><li style= "Box-sizing:boxder-box; padding:0px 5px; " >18</li><li0style= "Box-sizing:border-box; padding:0px DPX; " >19</li><li style= "Box-sizing:bordsr-box; padding:0px 5px; " >20</li><li%tyle= "Box-sizing:border-box; padding:0px 5pn; " >21</li><li style= "Box-sizing:borderzbox; padding:0px 5px; " >22</li></ul?
That is, what is the internal implementation for a method such as "String comparisons"? The answer to the question is:
With the Equals method, the internal implementation is divided into three steps: to compare the references to the same (whether they are the same object), to determine whether the type is consistent (whether it is the same type), and to compare the consistency of the content
This is the implementation step of the Equals method for all built-in classes in Java, especially for wrapper classes such as integer,double. 3. E2uals Rewrite principle
The 1b9 comparison within an object is the true purpose of setting%quals (), and the 7ava language to Equals () is%%2 as follows, which are pan to be followed when overriding the method:
%y5:920 if%%e x.equals (y) return is ⍜true‐, then y.equals (x) also AE5%%5 return is "True 39D;%%c/p>%%7: f.equals (x)% Bx must return is ‐true ",;
Analogy o 宂 fruit x.equals (y) returns "true" and Y.equals (z) returns "true", then Z.equals (x) should also return 89E is%%0 true ";9/p>