From a96eed84ca0d7a75ca81acff3e92177669fbd851 Mon Sep 17 00:00:00 2001
From: "Dr. Martin Goik" <goik@hdm-stuttgart.de>
Date: Tue, 20 Apr 2021 21:19:09 +0200
Subject: [PATCH] Better string concatenation explanation

---
 Doc/Sd1/languageFundamentals.xml | 109 +++++++++++++++++--------------
 Doc/Sd1/statements.xml           |  36 +++++++---
 2 files changed, 88 insertions(+), 57 deletions(-)

diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml
index c681408c5..9f4c24b23 100644
--- a/Doc/Sd1/languageFundamentals.xml
+++ b/Doc/Sd1/languageFundamentals.xml
@@ -3811,10 +3811,49 @@ System.out.println(11 + 22 + " is our result");</programlisting></td>
                 <para>Explain the different outcomes.</para>
 
                 <tip>
-                  <para>Read about the <quote>+</quote> operator's role
-                  <abbrev>e.g.</abbrev> in <code
-                  language="java">System.out.println("Some" + "
-                  string").</code></para>
+                  <para>Depending on its two operands different
+                  <quote>+</quote> operators are being chosen by your
+                  compiler:</para>
+
+                  <glosslist>
+                    <glossentry>
+                      <glossterm><code language="java">4 +
+                      3</code></glossterm>
+
+                      <glossdef>
+                        <para>The <quote>usual</quote> arithmetic sum operator
+                        resulting in the integer value 7.</para>
+                      </glossdef>
+                    </glossentry>
+                  </glosslist>
+
+                  <glosslist>
+                    <glossentry>
+                      <glossterm><code language="java">"Hello," + "
+                      World"</code></glossterm>
+
+                      <glossdef>
+                        <para>The string concatenation operator resulting in
+                        the String "Hello, World".</para>
+                      </glossdef>
+                    </glossentry>
+                  </glosslist>
+
+                  <glosslist>
+                    <glossentry>
+                      <glossterm><code language="java">"Current temperature: "
+                      + 20</code></glossterm>
+
+                      <glossdef>
+                        <para>The string concatenation operator again. Behind
+                        the scenes the integer value of 20 is being converted
+                        into the string <code language="java">"20"</code>
+                        prior to being concatenated into a single <code
+                        language="java">"Current temperature: 20"</code>
+                        string.</para>
+                      </glossdef>
+                    </glossentry>
+                  </glosslist>
                 </tip>
               </listitem>
 
@@ -3847,53 +3886,27 @@ System.out.println("Decimal:" +  (512 +
                 <para>We start by considering
                 <code>System.out.println("Result: " + 11 + 22)</code>.
                 Expressions involving the + operator are being evaluated from
-                left to right. The following two expressions are thus
-                equivalent:</para>
-
-                <itemizedlist>
-                  <listitem>
-                    <para><code>"Result: " + 11 + 22</code></para>
-                  </listitem>
-
-                  <listitem>
-                    <para><code>("Result: " + 11) + 22</code></para>
-                  </listitem>
-                </itemizedlist>
-
-                <para>The integer literal 11 within <code>"Result: " +
-                11</code> will be converted into a string <code>"11"</code>
-                prior to being concatenated <code>with the "Result: "</code>
-                string into <code>a single "Result: 11"</code> string.</para>
-
-                <para>Likewise in a second step <code>the intermediate
-                "Result: 11"</code> string and the again converted
-                <code>"22"</code> will be concatenated into the final "Result:
-                1122" string.</para>
-
-                <para>Regarding <code>System.out.println(11 + 22 + " is our
-                result")</code> on the other hand the following two
-                expressions are equivalent:</para>
-
-                <itemizedlist>
-                  <listitem>
-                    <para><code>11 + 22 + " is our result"</code></para>
-                  </listitem>
-
-                  <listitem>
-                    <para><code>(11 + 22) + " is our result"</code></para>
-                  </listitem>
-                </itemizedlist>
-
-                <para>This time the expression <code>11 + 22</code> containing
-                <quote>pure</quote> integer arithmetics results in an integer
-                value of 33. This value will be turned into a "33" string
-                before being concatenated with <code>" is our result"</code>
-                into the final output string.</para>
+                left to right. The result is thus:</para>
+
+                <programlisting language="none">System.out.println("Result: " + 11 + 22);
+<emphasis role="red">                       ╲       ╱     ╱   
+                      "Result: 11"  ╱  
+                          ╲        ╱
+                        "Result: 1122"</emphasis></programlisting>
+
+                <para>The second line will be evaluated in a different
+                manner:</para>
+
+                <programlisting language="none">System.out.println(11 + 22 + " is our result");
+<emphasis role="red">                     ╲ ╱           ╱   
+                      33          ╱  
+                        ╲        ╱
+                     "33 is our result"</emphasis></programlisting>
               </listitem>
 
               <listitem>
-                <para>We start by omitting the <quote>inner</quote>
-                braces:</para>
+                <para>We start by omitting the <quote>inner</quote> braces.
+                This results in:</para>
 
                 <informaltable border="1">
                   <tr>
diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml
index b3035a4e5..f3d2b63c2 100644
--- a/Doc/Sd1/statements.xml
+++ b/Doc/Sd1/statements.xml
@@ -307,7 +307,7 @@ Done!</screen></td>
             compile time error since we cannot assign a value to a
             literal:</para>
 
-            <programlisting language="java">if (4 == variable) {...}</programlisting>
+            <programlisting language="java">if (4 = variable) {...} // Compile time error in C/C++ as well</programlisting>
 
             <para>We are thus able to avoid this type of error in the first
             place.</para>
@@ -341,8 +341,7 @@ System.out.println(a + "+" + b+ "=" + (a + b));</programlisting></td>
                 </tr>
               </informaltable>
 
-              <para>Unfortunately changing the two variables' values
-              yields:</para>
+              <para>Unfortunately a negative value yields:</para>
 
               <informaltable border="0">
                 <tr>
@@ -362,7 +361,8 @@ System.out.println(a + "+" + b + "=" + (a + b));</programlisting></td>
               </informaltable>
 
               <para>This result looks awkward. Modify the code to see
-              <code>100-4=96</code> in such cases.</para>
+              <code>100-4=96</code> in such cases. You may reconsider the
+              findings from <xref linkend="sd1QandaBracesInPrintln"/>.</para>
             </question>
 
             <answer>
@@ -392,13 +392,32 @@ if (b &lt; 0) {
               <para>Since <code language="java">a</code> and <code
               language="java">b</code> are both variables of type <code
               language="java">int</code> they get added rather than string
-              style concatenated. Resolving this issue may be effected by
-              adding an empty string <coref
+              style concatenated:</para>
+
+              <programlisting language="none">System.out.println(a + b + "=" + (a + b));
+                    <emphasis role="red">╲  ╱    ╱      ╲ ╱ 
+                     96    ╱        96
+                       ╲  ╱        ╱ 
+                       "96="      ╱ 
+                          ╲      ╱ 
+                           "96=96"</emphasis></programlisting>
+
+              <para> Resolving this issue may be effected by adding an empty
+              string <coref
               linkend="sd1_qanda_betterSimpleMathEmptyString-1-co"/> forcing
               <xref linkend="glo_Java"/> to use the concatenation
               <quote>+</quote> operator in favour of the arithmetic
               one:</para>
 
+              <programlisting language="none">System.out.println(a + ""<co
+                  xml:id="sd1_qanda_betterSimpleMathEmptyString-1-co"/> + b + "=" + (a + b));
+                    <emphasis role="red">╲ ╱       ╱            ╲ ╱ 
+                   "100"     ╱             96
+                      ╲     ╱             ╱ 
+                       "100-4"           ╱ 
+                             ╲          ╱ 
+                              "100-4=96"</emphasis></programlisting>
+
               <informaltable border="0">
                 <tr>
                   <th>Source code</th>
@@ -411,8 +430,7 @@ if (b &lt; 0) {
     b = -4;
 
 if (b &lt; 0) {
-    System.out.println(a + "" <co
-                        xml:id="sd1_qanda_betterSimpleMathEmptyString-1-co"/> + b+ "=" + (a + b));
+    System.out.println(a + ""  + b + "=" + (a + b));
   } else {
     System.out.println(a + "+" + b + "=" + (a + b));
 }</programlisting></td>
@@ -6609,7 +6627,7 @@ Found (6, 8, 10)
                 either a == 0 or b == 0;</para>
 
                 <para>We create all possible combinations by using three
-                nested loops and filtering for the desired results: </para>
+                nested loops and filtering for the desired results:</para>
 
                 <programlisting language="java">final int LIMIT = 100;
 
-- 
GitLab