From 03f794b5b613045586c3ae96e8fdb2f86f698002 Mon Sep 17 00:00:00 2001
From: Martin Goik <goik@hdm-stuttgart.de>
Date: Fri, 7 Jul 2017 12:31:10 +0200
Subject: [PATCH] Java visualizer URL update

---
 Doc/Sd1/languageFundamentals.xml | 203 ++++++++++++++++++++++++++++---
 1 file changed, 188 insertions(+), 15 deletions(-)

diff --git a/Doc/Sd1/languageFundamentals.xml b/Doc/Sd1/languageFundamentals.xml
index f93124ec7..081363a13 100644
--- a/Doc/Sd1/languageFundamentals.xml
+++ b/Doc/Sd1/languageFundamentals.xml
@@ -60,9 +60,9 @@
       </mediaobject>
     </figure>
 
-    <para>The sample devices shown differ heavily with respect to addressable
-    memory, data size, arithmetic computation speed and other features. We
-    take a closet look to Zilog's Z80 processor:</para>
+    <para>These sample devices differ heavily with respect to addressable
+    memory, data size, supported arithmetic operations / speed and other
+    features. We take a closer look to Zilog's Z80 processor:</para>
 
     <figure xml:id="sd1_fig_z80">
       <title>Zilog Z80 CPU</title>
@@ -239,6 +239,105 @@
     You</quote> for further details.</para>
   </section>
 
+  <section xml:id="sd1_sect_variables">
+    <title>Variables</title>
+
+    <para>Declaring a variable requires a type name like <code>double</code>
+    and an identifier:</para>
+
+    <figure xml:id="sd1_fig_varDeclare">
+      <title>Variable declaration</title>
+
+      <programlisting language="java">double pi;</programlisting>
+    </figure>
+
+    <para>We may assign values to variables or build expressions like <code>pi
+    * 2.0 * 2.0</code> :</para>
+
+    <figure xml:id="sd1_fig_varDeclareAndUse">
+      <title>Declare and use</title>
+
+      <programlisting language="java">double pi; // Declaration
+...
+pi = 3.1415926; // Value assignment
+
+// Area of circle having radius 2.0
+System.out.println(pi * 2.0. * 2.0);</programlisting>
+    </figure>
+
+    <figure xml:id="sd1_fig_varDeclareInit">
+      <title>Declaration and initialization</title>
+
+      <glosslist>
+        <glossentry>
+          <glossterm>Separating declaration and initialization</glossterm>
+
+          <glossdef>
+            <programlisting language="java">double pi; // Declaration of variable pi
+...
+pi = 3.1415926; // Value assignment</programlisting>
+          </glossdef>
+        </glossentry>
+
+        <glossentry>
+          <glossterm>Combining declaration and initialization</glossterm>
+
+          <glossdef>
+            <programlisting language="java">double pi = 3.1415926;</programlisting>
+          </glossdef>
+        </glossentry>
+      </glosslist>
+    </figure>
+
+    <figure xml:id="sd1_fig_typeSafety">
+      <title>Type safety</title>
+
+      <programlisting language="java">int i = 2;
+int j = i; // o.K.: Assigning int to int
+
+boolean b = true;
+i = b;     // Error: int and boolean are incompatible types
+
+i = "Hello"; // Even worse: Assigning a String to an int</programlisting>
+    </figure>
+
+    <figure xml:id="sd1_fig_variableCategories">
+      <title>Two categories of variables</title>
+
+      <glosslist>
+        <glossentry>
+          <glossterm>Primitive type</glossterm>
+
+          <glossdef>
+            <programlisting language="java">int a = -15;</programlisting>
+
+            <para>Possible types: All eight primitive <xref
+            linkend="glo_Java"/> types.</para>
+          </glossdef>
+        </glossentry>
+
+        <glossentry>
+          <glossterm>Reference type</glossterm>
+
+          <glossdef>
+            <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829);</programlisting>
+
+            <para>Possible types: Arbitrary built in or user defined
+            classes.</para>
+          </glossdef>
+        </glossentry>
+      </glosslist>
+    </figure>
+
+    <figure xml:id="sd1_fig_refTypeVariants">
+      <title>Reference type variants</title>
+
+      <programlisting language="java">GpsPosition start = new GpsPosition(48.7758, 9.1829);
+String name = "Simon";
+LocalDate birtday = LocalDate.of(1990, Month.JULY, 5);</programlisting>
+    </figure>
+  </section>
+
   <section xml:id="sw1LegalVariableName">
     <title>Legal variable names</title>
 
@@ -1567,6 +1666,71 @@ Seconds:31</programlisting>
       </qandaset>
     </section>
 
+    <section xml:id="sd1_sect_typeAssignCompat">
+      <title>Assignment and type safety</title>
+
+      <qandaset defaultlabel="qanda" xml:id="sd1_qanda_typeAssignCompat">
+        <qandadiv>
+          <qandaentry>
+            <question>
+              <para>We consider:</para>
+
+              <programlisting language="java">int i = -21;
+double d = i;</programlisting>
+
+              <para>This snippet shows perfectly correct <xref
+              linkend="glo_Java"/> code. However <xref
+              linkend="sd1_fig_typeSafety"/> suggests we should not be allowed
+              to assign an <code>int</code> to a <code>double</code> value due
+              to type safety.</para>
+
+              <para>Even worse: Swapping types yields a compile time
+              error:</para>
+
+              <programlisting language="java">double d = 2.4;
+int i = d; // Error: Incompatible types</programlisting>
+
+              <para>Why is this? Make an educated guess.</para>
+            </question>
+
+            <answer>
+              <para>When assigning <code>d = i</code> the <xref
+              linkend="glo_Java"/> compiler will implicitly convert the
+              <code>int</code> into a <code>double</code> value.</para>
+
+              <para>Turning a double into an int is more cumbersome: The
+              expression <code>i = 3.5</code> could be evaluated by applying
+              agreeing on a specific rounding prescription. But what about i =
+              3457357385783573478955345.45? A <xref linkend="glo_Java"/>
+              <code>int</code>'s maximum value is <inlineequation>
+                  <m:math display="inline">
+                    <m:mrow>
+                      <m:msup>
+                        <m:mi>2</m:mi>
+
+                        <m:mi>31</m:mi>
+                      </m:msup>
+
+                      <m:mo>-</m:mo>
+
+                      <m:mi>1</m:mi>
+                    </m:mrow>
+                  </m:math>
+                </inlineequation>exceeding the former <code>double</code>
+              value.</para>
+
+              <para>Conclusion: <xref linkend="glo_Java"/> disallows double to
+              int assignments unless using a so called cast (explicit type
+              conversion):</para>
+
+              <programlisting language="java">double d = 2.4;
+int i = (int) d; // Explicit cast double to int</programlisting>
+            </answer>
+          </qandaentry>
+        </qandadiv>
+      </qandaset>
+    </section>
+
     <section xml:id="sd1SectLong2Float">
       <title>Reducing <code>long</code> to <code>int</code></title>
 
@@ -1719,21 +1883,30 @@ System.out.println(reducedValue);</programlisting>
 
               <orderedlist>
                 <listitem>
-                  <para>The expression <code>Integer.MAX_VALUE + 1</code> adds
-                  two int values thus returning <code>Integer.MIN_VALUE</code>
-                  due to a non-intended arithmetic overflow.</para>
-                </listitem>
-
-                <listitem>
-                  <para>Even if the above problem was inexistent the
-                  expression <code>2 * (Integer.MAX_VALUE + 1)</code> also
-                  gives rise to a second overflow error.</para>
+                  <para>The constant <code>Integer.MAX_VALUE</code> already
+                  suggests we will not be able to increase its value while
+                  staying as an <code>int</code>. The expression
+                  <code>Integer.MAX_VALUE + 1</code> indeed returns
+                  <code>Integer.MIN_VALUE</code> due to an arithmetic
+                  overflow:</para>
+
+                  <programlisting language="java">  01111111_11111111_11111111_11111111
++ 00000000_00000000_00000000_00000000
+_____________________________________
+  10000000_00000000_00000000_00000000</programlisting>
+
+                  <para>The expression <code>2 * (Integer.MAX_VALUE +
+                  1)</code> then gives rise to a second overflow error:</para>
+
+                  <programlisting language="java">  10000000_00000000_00000000_00000000
++ 10000000_00000000_00000000_00000000
+_____________________________________
+  00000000_00000000_00000000_00000000</programlisting>
                 </listitem>
               </orderedlist>
 
-              <para>Both errors combined (surprisingly) result in <code>2 *
-              (Integer.MAX_VALUE + 1)</code> evaluating to <code>0</code>.
-              There are two possible solutions:</para>
+              <para>Both errors combined (surprisingly) result in a value of
+              <code>0</code>. There are two possible solutions:</para>
 
               <glosslist>
                 <glossentry>
-- 
GitLab