From d0b4a9114b270fc3fe11ea3d9307872b613ae0ec Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Wed, 29 Nov 2017 11:11:17 +0100 Subject: [PATCH] Reference types --- Doc/Sd1/objectsClasses.xml | 178 ++++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 1 deletion(-) diff --git a/Doc/Sd1/objectsClasses.xml b/Doc/Sd1/objectsClasses.xml index 9f5bc07c6..3d651a8ae 100644 --- a/Doc/Sd1/objectsClasses.xml +++ b/Doc/Sd1/objectsClasses.xml @@ -4040,7 +4040,6 @@ public class <link balance = balance * Math.pow((1 + defaultInterestRate / 100), years) ; } }</programlisting> - </answer> </qandaentry> </qandadiv> @@ -8189,6 +8188,183 @@ sin(4 * PI)=4518.2187229323445, difference=4518.2187229323445</screen> </section> </section> + <section xml:id="sda1_sect_valueVsReferenceType"> + <title>Value types and reference types</title> + + <figure xml:id="sd1_fig_valueRefType"> + <title>Value vs. reference types</title> + + <glosslist> + <glossentry> + <glossterm>Value type</glossterm> + + <glossdef> + <para>Holding data within own memory location.</para> + + <para>Eight value types in <xref linkend="glo_Java"/>: + <code>byte</code>, <code>short</code>, <code>int</code>, + <code>long</code>, <code>float</code>, <code>double</code>, + <code>char</code> and <code>boolean</code>.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Reference type</glossterm> + + <glossdef> + <para>All class instances i.e. <classname>String</classname>, + <classname>java.util.Scanner</classname> etc.</para> + </glossdef> + </glossentry> + </glosslist> + </figure> + + <figure xml:id="sd1_fig_valueRefExample"> + <title>Example</title> + + <informaltable border="1"> + <colgroup width="76%"/> + + <colgroup width="24%"/> + + <tr> + <td valign="top"><programlisting language="java">int a = 1; +int b = a; +a = 5; +System.out.println( + "a="+a+"\nb=" + b);</programlisting></td> + + <td valign="top"><screen>a=5 +b=1 </screen></td> + </tr> + + <tr> + <td valign="top"><programlisting language="java">StringBuffer r = new StringBuffer("Joe"); +StringBuffer s = r; +r.append(" Simpson"); + +System.out.println("r="+r+"\ns="+s);</programlisting></td> + + <td valign="top"><screen>r=Joe Simpson +s=Joe Simpson </screen></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_callByValueExample"> + <title><emphasis>Only</emphasis> »call-by-value« in <xref + linkend="glo_Java"/></title> + + <informaltable border="1"> + <colgroup width="69%"/> + + <colgroup width="31%"/> + + <tr> + <td valign="top"><programlisting language="java">public static void main(String[] args) { + int value = 3; + System.out.println("Before printDuplicateValue: " + value); + printDuplicateValue(value); + System.out.println("After printDuplicateValue: " + value); +} +static void printDuplicateValue(int n) { + n = 2 * n; + System.out.println("printDuplicateValue: " + n); +}</programlisting></td> + + <td valign="top"><screen>Before printDuplicateValue: <emphasis + role="red">3</emphasis> +printDuplicateValue: <emphasis role="red">6</emphasis> +After printDuplicateValue: <emphasis role="red">3</emphasis></screen></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_callByValueStringBuffer"> + <title><emphasis>»call-by-reference« for objects?</emphasis></title> + + <informaltable border="1"> + <colgroup width="66%"/> + + <colgroup width="34%"/> + + <tr> + <td valign="top"><programlisting language="java">public static void main(String[] args) { + StringBuffer buffer = new StringBuffer("My"); + System.out.println("Before duplicateString: " + buffer); + duplicateString(buffer); + System.out.println("After duplicateString: " + buffer); + +} +static void duplicateString(StringBuffer b) { + b.append(b); // Append self +}</programlisting></td> + + <td valign="top"><screen>Before duplicateString: <emphasis + role="red">My</emphasis> +After duplicateString: <emphasis role="red">MyMy </emphasis></screen></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_NoCallByReferenceInJava"> + <title><emphasis>No »call-by-reference« in <xref + linkend="glo_Java"/></emphasis></title> + + <informaltable border="1"> + <colgroup width="66%"/> + + <colgroup width="34%"/> + + <tr> + <td valign="top"><programlisting language="java">public static void main(String[] args) { + StringBuffer buffer = new StringBuffer("My"); + System.out.println("Before duplicateString: " + buffer); + replaceString(buffer); + System.out.println("After duplicateString: " + buffer); +} +static void replaceString(StringBuffer b) { + b = new StringBuffer("Replacement"); +}</programlisting></td> + + <td valign="top"><screen>Before duplicateString: <emphasis + role="red">My</emphasis> +After duplicateString: <emphasis role="red">My</emphasis></screen></td> + </tr> + </informaltable> + </figure> + + <figure xml:id="sd1_fig_CallByReferenceCpp"> + <title><emphasis>C++ offers <quote>true</quote> + »call-by-reference«</emphasis></title> + + <informaltable border="1"> + <colgroup width="69%"/> + + <colgroup width="31%"/> + + <tr> + <td valign="top"><programlisting language="none">void printDuplicateValue(int<emphasis + role="red">&</emphasis> n) { // alternate name + n = 2 * n; + cout << "printDuplicateValue: " << n << endl; +} +int main() { + int value = 3; + cout << "Before printDuplicateValue: " << value << endl; + printDuplicateValue(value); + cout << "Before printDuplicateValue: " << value << endl; +}</programlisting></td> + + <td valign="top"><screen>Before printDuplicateValue: <emphasis + role="red">3</emphasis> +printDuplicateValue: <emphasis role="red">6</emphasis> +After printDuplicateValue: <emphasis role="red">6</emphasis></screen></td> + </tr> + </informaltable> + </figure> + </section> + <section xml:id="set_sd1_methodCallingDetails"> <title>Method calls, the details</title> -- GitLab