From 28fda45b76e729fe5419593d24bc4b2d8f99037d Mon Sep 17 00:00:00 2001 From: "Dr. Martin Goik" <goik@hdm-stuttgart.de> Date: Tue, 19 Feb 2019 13:39:27 +0100 Subject: [PATCH] Cosmetics --- Doc/Sd1/coreClasses.xml | 47 ++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/Doc/Sd1/coreClasses.xml b/Doc/Sd1/coreClasses.xml index 44f0d046f..5bd157eba 100644 --- a/Doc/Sd1/coreClasses.xml +++ b/Doc/Sd1/coreClasses.xml @@ -377,26 +377,39 @@ equals: true</screen></td> </question> <answer> + <para>According to <xref + linkend="sd1_coreclasses_fig_hashBasicIdea"/> part of a + <methodname>hashCode() methods contract is:</methodname></para> + + <para><code language="java">true == a.equals(b)</code> ⟹ <code + language="java">a.hashCode() == b.hashCode()</code>.</para> + <para>An ideal/perfect <methodname>hashCode()</methodname> method - in addition to <xref linkend="sd1_coreclasses_fig_hashBasicIdea"/> - will return different values whenever two instances <code>a</code> - and <code>b</code> differ in value with respect to the underlying - <methodname>equals()</methodname> method:</para> + in addition will return different values whenever two instances + <code>a</code> and <code>b</code> differ in value with respect to + the underlying <methodname>equals()</methodname> method:</para> - <para><code language="java">false == a.equals(b)</code> ⟹ <code + <para><code language="java">false == a.equals(b)</code> ⟺ <code language="java">a.hashCode() != b.hashCode()</code>.</para> + <para>Combining these two statements a perfect hashCode() method + will have the following property with respect to its corresponding + <methodname>equals()</methodname> method:</para> + + <para><code language="java">a.equals(b) == (a.hashCode() == + b.hashCode())</code></para> + <para>Method 1 serves this purpose: Its returns a given - <classname>TimePeriod</classname> instance total amount of seconds - equivalent e.g turning (<emphasis role="rd">1</emphasis> hour, - <emphasis role="rd">2</emphasis> minutes, <emphasis - role="rd">5</emphasis> seconds) into (<emphasis + <classname>TimePeriod</classname> instance into its total amount + of seconds equivalent. Example: (<emphasis role="rd">1</emphasis> + hour, <emphasis role="rd">2</emphasis> minutes, <emphasis + role="rd">5</emphasis> seconds) amounts to (<emphasis role="rd">1</emphasis> * 60 + <emphasis role="rd">2</emphasis>) * - 60 + <emphasis role="rd">5</emphasis> = 3725 seconds. Due its - specification a <classname>TimePeriod</classname> instance cannot - exceed 24 hours being equivalent to 24 * 60 * 60 = 86400 seconds. - A 4 byte <code language="java">int</code> is therefore safe with - respect to overflow issues.</para> + 60 + <emphasis role="rd">5</emphasis> = 3725 seconds. Due to its + specification an instance of <classname>TimePeriod</classname> + cannot exceed 24 hours being equivalent to 24 * 60 * 60 = 86400 + seconds. Thus a 4-byte <code language="java">int</code> is safe + with respect to overflow issues.</para> <para>Conclusion: Whenever two instances of <classname>TimePeriod</classname> differ their method 1 @@ -407,9 +420,9 @@ equals: true</screen></td> instances (1 hour, 2 minutes, 3 seconds) vs. (3 hours, 2 minutes, 1 second) returning an identical <methodname>hashCode()</methodname> of 1 + 2 + 3 = 3 + 2 + 1 = 6. - Thus method 2 requiring just two additions offers (slightly) - better runtime performance at the expense of a higher hash value - collision rate. </para> + So method 2 requiring just two additions offers (slightly) better + runtime performance at the expense of a higher hash value + collision rate.</para> </answer> </qandaentry> </qandadiv> -- GitLab