From 81d57e8e91c538c13052194b803b17cf8839429d Mon Sep 17 00:00:00 2001 From: "Dr. Martin Goik" <goik@hdm-stuttgart.de> Date: Sun, 3 Nov 2019 18:16:11 +0100 Subject: [PATCH] Further void return related explanations --- Doc/Sd1/objectsClasses.xml | 57 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index e955ead33..717749e8d 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -884,6 +884,21 @@ System.out.println("Perimeter=" + r.getPerimeter());</programlisting></td> <para>You'll encounter a <quote>Missing return statement</quote> error. What's wrong here?</para> + + <para>On contrary the following code compiles and executes + perfectly well:</para> + + <programlisting language="java">public class ReturnDemo { + + int minimum; + + public void getMinimum(int a, int b) { + if (a < b) { + minimum = a; + return; + } + } +}</programlisting> </question> <answer> @@ -914,6 +929,42 @@ System.out.println("Perimeter=" + r.getPerimeter());</programlisting></td> }</programlisting> </listitem> </orderedlist> + + <para>On the other hand the return statement in + <classname>ReturnDemo</classname>.<methodname>getMinimum(...)</methodname> + is unnecessary. The following code produces exactly the same + result. The method will terminate anyway when ending the method's + body:</para> + + <programlisting language="java">public class ReturnDemo { + + int minimum; + + public void getMinimum(int a, int b) { + if (a < b) { + minimum = a; // Same as before but no subsequent return statement + } + } +}</programlisting> + + <para>Albeit executing well the above code is flawed: Calling e.g. + <methodname>getMinimum(4, 3)</methodname> does not assign any + value to our instance variable <code + language="java">minimum</code>. Correction requires an <code + language="java">else</code> clause:</para> + + <programlisting language="java">public class ReturnDemo { + + int minimum; + + public void getMinimum(int a, int b) { + if (a < b) { + minimum = a; // Same as before but no subsequent return statement + } else { + minimum = b; + } + } +}</programlisting> </answer> </qandaentry> </qandadiv> @@ -1016,7 +1067,7 @@ System.out.println("Failed!");</programlisting> xlink:href="https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/">Apple goto fail SSL bug</link> could not have been occurring in <xref linkend="glo_Java"/> due to an inherent language - feature. </para> + feature.</para> </listitem> <listitem> @@ -1074,8 +1125,8 @@ System.out.println("Failed!");</programlisting> <para>Set your compiler / <xref linkend="glo_IDE"/>'s warnings to a yet tolerable higher level. In case of IntelliJ IDEA you may want to activate various settings in your project's - <quote>Java</quote> section below Editor --> Inspections. - </para> + <quote>Java</quote> section below Editor --> + Inspections.</para> </important> </answer> </qandaentry> -- GitLab