diff --git a/Doc/Sd1/Ref/CoreClasses/Hash/iceCreamPrices.jpg b/Doc/Sd1/Ref/CoreClasses/Hash/iceCreamPrices.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5d9a6ba03d7b144e5cef3c49dd93e404087698b0 Binary files /dev/null and b/Doc/Sd1/Ref/CoreClasses/Hash/iceCreamPrices.jpg differ diff --git a/Doc/Sd1/coreClasses.xml b/Doc/Sd1/coreClasses.xml index 40b0cae5dcde35b6d2bd038f8fdae5c0fc1fd39c..28e2871e14dade0ad150af9b4c8cc3de8eaa2db7 100644 --- a/Doc/Sd1/coreClasses.xml +++ b/Doc/Sd1/coreClasses.xml @@ -2,6 +2,7 @@ <chapter annotations="slide" version="5.1" xml:id="sw1ChapterCoreClasses" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" + xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns="http://docbook.org/ns/transclusion" @@ -75,13 +76,131 @@ String s2 = new String("Kate"); System.out.println("s1 == s2: " + (s1 == s2)); -System.out.println("s1.equals(s2): " + s1.equals(s2)); -</programlisting> +System.out.println("s1.equals(s2): " + s1.equals(s2));</programlisting> <screen>s1 == s2: false s1.equals(s2): true</screen> </figure> + <figure xml:id="sd1_coreclasses_fig_hashPrinciple"> + <title>Hashing principle</title> + + <informaltable border="0"> + <tr> + <td valign="top"><mediaobject> + <imageobject> + <imagedata fileref="Ref/CoreClasses/Hash/iceCreamPrices.jpg"/> + </imageobject> + </mediaobject></td> + + <td valign="middle"><quote>I want the 12p one</quote></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_coreclasses_fig_hashExample"> + <title><classname>Rectangle</classname> + <methodname>equals(...)</methodname> and + <methodname>hashCode()</methodname></title> + + <programlisting language="java">public class Rectangle { + int width, height; + @Override public boolean equals(Object o) { + if (o instanceof Rectangle) { + return width == ((Rectangle) o).width + && height == ((Rectangle) o).height; + } else { + return false; + } + } + @Override public int hashCode() { + return width + height; + } +}</programlisting> + </figure> + + <figure xml:id="sd1_coreclasses_fig_hashRectangleValues"> + <title><classname>Rectangle</classname> hash values</title> + + <informaltable border="1"> + <tr> + <th>width</th> + + <th>height</th> + + <th>hash value</th> + </tr> + + <tr> + <td valign="top">1</td> + + <td valign="top">3</td> + + <td valign="top">4</td> + </tr> + + <tr> + <td valign="top">2</td> + + <td valign="top">2</td> + + <td valign="top">4</td> + </tr> + + <tr> + <td valign="top">5</td> + + <td valign="top">5</td> + + <td valign="top">10</td> + </tr> + + <tr> + <td valign="top">2</td> + + <td valign="top">7</td> + + <td valign="top">9</td> + </tr> + + <tr> + <td valign="top">4</td> + + <td valign="top">9</td> + + <td valign="top">13</td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_coreclasses_fig_hashRectangleBetter"> + <title><classname>Rectangle</classname> + <methodname>equals(...)</methodname> and + <methodname>hashCode()</methodname></title> + + <programlisting language="java">public class Rectangle { + int width, height; +... + @Override public int hashCode() { + return width + <emphasis role="red">13 * height</emphasis>; + } +}</programlisting> + </figure> + + <figure xml:id="sd1_coreclasses_fig_hashEqualsContract"> + <title><classname>Contract + </classname><methodname>equals(...)</methodname> and + <methodname>hashCode()</methodname></title> + + <para>If <methodname>a.equals(b)</methodname> ==> <code>a.hashCode() == + b.hashCode()</code>. <emphasis role="red">The reverse does not + apply!</emphasis></para> + + <para>Consequence: <methodname>equals()</methodname> and + <methodname>hashCode()</methodname> must be <emphasis role="red">redefined + simultaneously</emphasis>!</para> + </figure> + <section xml:id="sd1SectVarargsFormat"> <title>Reconsidering System.out.format().</title>