diff --git a/Sd1/swd1.xml b/Sd1/swd1.xml index 4ac97a28721c8ee9553907c35c041bc6304bd099..0161eb45b0e5fe5bebca7092103042530f304eae 100644 --- a/Sd1/swd1.xml +++ b/Sd1/swd1.xml @@ -6506,9 +6506,119 @@ goik@goiki:~/workspace/life/src/main/java$ cp *.java ~/my-scenarios/Life</progra <chapter xml:id="sd1IdentEqual"> <title>Object identity and equality (3.12.)</title> + <section xml:id="sec_PrepareOidEquality"> + <title>Preparations</title> + + <para>Read all sections of chapter 6 in <xref linkend="bib_Horton2011"/> + till and including <quote>THE UNIVERSAL SUPERCLASS</quote>.</para> + </section> + <section xml:id="sd1String2Exercises"> <title>Exercises</title> + <section xml:id="pitfallsUsingOperatorEquals"> + <title>Pitfalls using operator <quote>==</quote></title> + + <qandaset defaultlabel="qanda" xml:id="qandaStringOperatorEquals"> + <title>String instances and equality</title> + + <qandadiv> + <qandaentry> + <question> + <para>Consider the following fragment:</para> + + <programlisting language="java">public static void main(String[] args) { + + final String a1 = "TestA", a2 = "TestA"; + System.out.println(" a1 == a2: " + (a1 == a2)); + + final String b1 = new String("TestB"), b2 = new String("TestB"); + System.out.println("b1 == b2: " + (b1 == b2)); +}</programlisting> + + <para>Execute this code and explain the resulting + output.</para> + + <para>Hints: Read the documentation of <link + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#identityHashCode-java.lang.Object-">System.identityHashCode(Object + o)</link> and <link + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--">Object.hashCode()</link>.</para> + </question> + + <answer> + <para>Execution yields:</para> + + <programlisting language="none">a1 == a2: true <co + linkends="answerCoStringOperatorEquality-1" + xml:id="answerCoStringOperatorEquality-1-co"/> +b1 == b2: false <co linkends="answerCoStringOperatorEquality-2" + xml:id="answerCoStringOperatorEquality-2-co"/></programlisting> + + <calloutlist> + <callout arearefs="answerCoStringOperatorEquality-1-co" + xml:id="answerCoStringOperatorEquality-1"> + <para>This effectively compares two string literals + <code>"TestA"</code> and <code>"TestA"</code>. The <xref + linkend="glo_JDK"/> compiler implementation allocates only + one instance of class String for all string literals + having identical content. So all string literals "TestA" + (even if existing in different classes or packages) + represent the same object. Thus the two distinct variables + <code>a1</code> and <code>a2</code> are being assigned an + identical reference pointing to this unique instance. + Comparing identical references by the operator == always + yields true. You might as well write:</para> + + <programlisting language="java">System.out.println(<emphasis + role="bold">"TestA".equals("TestA")</emphasis>);</programlisting> + + <para>The method <link + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/System.html#identityHashCode-java.lang.Object-">System.identityHashCode(Object + o)</link> returns <link + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--">Object.hashCode()</link> + rather then <link + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#hashCode--">String.hashCode()</link>. + This hash code from <link + xlink:href="http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html">java.lang.Object</link> + has a one to one correspondence to an object's reference + and thus helps to understand the underlying object + references of our current example:</para> + + <programlisting language="java">System.out.println("Hashcode a1 == " + System.identityHashCode(a1) + + ", Hashcode a2 == " + System.identityHashCode(a2));</programlisting> + + <para>This yields identical values showing that + <code>a1</code> and <code>a2</code> point to the same + instance:</para> + + <programlisting language="none">Hashcode a1 == 366712642, Hashcode a2 == 366712642</programlisting> + </callout> + + <callout arearefs="answerCoStringOperatorEquality-2-co" + xml:id="answerCoStringOperatorEquality-2"> + <para>b1 and b2 are being constructed as two different + instances albeit containing the same value. Following the + above reasoning we may execute:</para> + + <programlisting language="java">System.out.println("Hashcode b1 == " + System.identityHashCode(b1) + + ", Hashcode b2 == " + System.identityHashCode(b2));</programlisting> + + <para>This yields values corresponding to two different + object references:</para> + + <programlisting language="none">Hashcode b1 == 1829164700, Hashcode b2 == 2018699554</programlisting> + + <para>Comparing these two values for equality via the + <quote>==</quote> operator will return the boolean value + <code>false</code>.</para> + </callout> + </calloutlist> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + <section xml:id="sd1GomeOfLifeLogic"> <title>Implementing <quote>Game of Life</quote> logic</title>