<?xml version="1.0" encoding="UTF-8"?> <chapter annotations="slide" version="5.1" xml:id="sd1GettingStarted" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xila="http://www.w3.org/2001/XInclude/local-attributes" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns="http://docbook.org/ns/transclusion" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:db="http://docbook.org/ns/docbook"> <title>Getting started</title> <figure xml:id="sd1_fig_workload"> <title>How much time to invest?</title> <mediaobject> <imageobject> <imagedata fileref="Fig/weeklyWorkload.multi.svg"/> </imageobject> </mediaobject> </figure> <section xml:id="sw1Resources"> <title>Lecture related resources</title> <figure xml:id="sd1_fig_recommendedReading"> <title>Recommended reading resources I</title> <mediaobject> <imageobject> <imagedata fileref="Fig/recommendedReadings.multi.svg"/> </imageobject> </mediaobject> </figure> <figure xml:id="sd1_fig_recommendedReading2"> <title>Recommended reading resources II</title> <glosslist> <glossentry xml:id="glossSda1PrimaryLectures"> <glossterm>Primary</glossterm> <glossdef> <itemizedlist> <listitem> <para><link xlink:href="https://brainysoftware.com/book/9781771970365">Java: A Beginner's Tutorial (5th Edition)</link></para> </listitem> </itemizedlist> </glossdef> </glossentry> <glossentry xml:id="glossSda1SecondaryLectures"> <glossterm>Secondary</glossterm> <glossdef> <itemizedlist> <listitem> <para><link xlink:href="http://openbook.rheinwerk-verlag.de/javainsel" xml:lang="de"><productname>Java</productname> ist auch eine Insel</link> of 12-th edition book 2018 including <xref linkend="glo_Java"/> 11.</para> </listitem> <listitem> <para><link xlink:href="https://www.hanser-fachbuch.de/buch/Programmieren+in+Java/9783446441347">Programmieren in Java</link></para> </listitem> </itemizedlist> </glossdef> </glossentry> </glosslist> </figure> <figure xml:id="sd1_fig_biggestEnemy"> <title>Your biggest enemies</title> <mediaobject> <imageobject> <imagedata fileref="Fig/biggestEnemy.multi.svg"/> </imageobject> </mediaobject> </figure> <figure xml:id="sd1_fig_nurMalSchnell"> <title>German humour</title> <para xml:lang="de">Aus <link xlink:href="https://www.der-postillon.com/2021/01/smartphone.html"><quote>Der Postillion</quote></link> :</para> <blockquote xml:lang="de"> <para>Mann, der am Handy</para> <para>nur mal eben die Uhrzeit nachschauen wollte,</para> <para>chattet acht Minuten auf WhatsApp,</para> <para>schaut drei YouTube-Videos</para> <para>und liest einen Artikel über Peru,</para> <para>weiß aber am Ende immer noch nicht, wie spät es ist</para> </blockquote> </figure> <figure xml:id="sd1_fig_timeManagement"> <title>4 most imperative study objectives</title> <orderedlist> <listitem> <para><link xlink:href="https://www.intelligent.com/manage-your-time">MANAGE YOUR TIME!</link></para> </listitem> <listitem> <para><link xlink:href="https://www.mindtools.com/pages/main/newMN_HTE.htm">MANAGE YOUR TIME!</link></para> </listitem> <listitem> <para><link xlink:href="https://toggl.com/time-management-tips">MANAGE YOUR TIME!</link></para> </listitem> <listitem> <para><link xlink:href="https://www.psychologytoday.com/intl/basics/time-management">MANAGE YOUR TIME!</link></para> </listitem> </orderedlist> </figure> <figure xml:id="sd1_fig_onlineRurorial"> <title>Online tutorials</title> <glosslist> <glossentry> <glossterm><link xlink:href="http://programmedlessons.org/Java9/">Bradley Kjell: Introduction to Computer Science using Java</link></glossterm> <glossdef> <itemizedlist> <listitem> <para><link xlink:href="http://www.gailer-net.de/tutorials/java/java-toc.html">German translation by Heinrich Gailer</link></para> </listitem> </itemizedlist> </glossdef> </glossentry> <glossentry> <glossterm><link xlink:href="https://www.udemy.com/java-tutorial/learn/v4/content">Udemy: Java Tutorial for Complete Beginners</link></glossterm> <glossdef> <para>Video tutorials and related source code examples. Registration required.</para> </glossdef> </glossentry> </glosslist> </figure> <figure xml:id="sd1_fig_unixIntro"> <title>Unix and the terminal</title> <itemizedlist> <listitem> <para><link xlink:href="https://swcarpentry.github.io/shell-novice">The Unix Shell</link> / Software-carpentry, nice video collection. Each section is also available in PDF and <trademark>PowerPoint</trademark> format.</para> </listitem> <listitem> <para><link xlink:href="http://www.ee.surrey.ac.uk/Teaching/Unix">UNIX Tutorial for Beginners</link>, text oriented.</para> </listitem> <listitem> <para><link xlink:href="https://kb.iu.edu/d/afsk">Introduction to Unix commands</link></para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_programmingExercisesAutomatedFeedback"> <title>Online programming, automated feedback</title> <glosslist> <glossentry> <glossterm><link xlink:href="http://codingbat.com">http://codingbat.com</link></glossterm> <glossdef> <para>No registration required.</para> </glossdef> </glossentry> <glossentry> <glossterm><link xlink:href="https://www.programmr.com/zone/java">https://www.programmr.com/zone/java</link></glossterm> <glossdef> <itemizedlist> <listitem> <para>Hunt for <quote>Challenges</quote> within page.</para> </listitem> <listitem> <para>Registration required.</para> </listitem> </itemizedlist> </glossdef> </glossentry> <glossentry> <glossterm><link xlink:href="https://www.codewars.com">https://www.codewars.com</link></glossterm> <glossdef> <itemizedlist> <listitem> <para>Registration or <link xlink:href="https://github.com">github.com</link> login required.</para> </listitem> </itemizedlist> </glossdef> </glossentry> </glosslist> </figure> <figure xml:id="sd1_fig_programmingExercises"> <title>Online programming I</title> <glosslist> <glossentry> <glossterm><uri xlink:href="http://www.codeabbey.com">codeabbey.com</uri></glossterm> <glossdef> <para><link xlink:href="http://www.codeabbey.com/index/task_list">Problem list</link>.</para> </glossdef> </glossentry> <glossentry> <glossterm><uri xlink:href="https://rosettacode.org">rosettacode.org</uri></glossterm> <glossdef> <para><link xlink:href="https://rosettacode.org/wiki/Category:Programming_Tasks">Programming tasks</link> (including solutions for multiple languages).</para> </glossdef> </glossentry> <glossentry> <glossterm>reddit.com</glossterm> <glossdef> <para><link xlink:href="https://www.reddit.com/r/dailyprogrammer">Daily Programmer</link>.</para> </glossdef> </glossentry> </glosslist> </figure> <figure xml:id="sd1_fig_programmingExercises_2"> <title>Online programming II</title> <glosslist> <glossentry> <glossterm xlink:href="https://projecteuler.net">Project Euler</glossterm> <glossdef> <para>Registration required for keeping track of your exercises' status. The following exercises in particular are considered to be useful with respect to this lecture:</para> <para><link xlink:href="https://projecteuler.net/problem=1">1</link>, <link xlink:href="https://projecteuler.net/problem=2">2</link>, <link xlink:href="https://projecteuler.net/problem=4">4</link>, <link xlink:href="https://projecteuler.net/problem=5">5</link>, <link xlink:href="https://projecteuler.net/problem=8">8</link>, <link xlink:href="https://projecteuler.net/problem=9">9</link>, <link xlink:href="https://projecteuler.net/problem=11">11</link>.</para> </glossdef> </glossentry> <glossentry> <glossterm>Java Programming Tutorial</glossterm> <glossdef> <para xlink:href="https://www3.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html">Basic and more difficult exercises</para> </glossdef> </glossentry> <glossentry> <glossterm>Java Programming Exercises</glossterm> <glossdef> <para>Start from the <link xlink:href="https://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/java.html">easier exercises</link>.</para> </glossdef> </glossentry> </glosslist> </figure> <figure xml:id="sd1_fig_javaVisualizer"> <title>Java Visualizer</title> <mediaobject> <imageobject> <imagedata fileref="Fig/javaVisualizer.svg"/> </imageobject> </mediaobject> <para><link xlink:href="https://prog.mi.hdm-stuttgart.de/java_visualize">http://prog.mi.hdm-stuttgart.de/java_visualize</link></para> </figure> <figure xml:id="sd1_fig_liveLectureAdditions"> <title>Live <link xlink:href="https://cloud.mi.hdm-stuttgart.de/index.php/s/df9f296af3298f96361a15a679390e59">lecture additions</link></title> <mediaobject> <imageobject> <imagedata fileref="Fig/liveLectureAdditions.svg"/> </imageobject> </mediaobject> </figure> <figure xml:id="sd1_fig_IntelliJidea"> <title><xref linkend="glo_Soft_IntellijIDEA"/> <xref linkend="glo_IDE"/> (See <link linkend="sd1_sect_usingMavenIdea">installation details)</link></title> <mediaobject> <imageobject> <imagedata fileref="Fig/idea.svg"/> </imageobject> </mediaobject> </figure> <figure xml:id="sd1_fig_MiLinuxImages"> <title><productname xlink:href="https://www.virtualbox.org">Virtualbox</productname> / <productname xlink:href="https://www.vmware.com/products/workstation-player.html">VMware</productname> player based virtualized Linux image</title> <itemizedlist> <listitem> <para>Contain all MI pool workstation Linux software.</para> </listitem> <listitem> <para>Available for free <productname xlink:href="https://www.vmware.com/products/workstation-pro/workstation-pro-evaluation.html">VMware Workstation Player</productname> as <link xlink:href="https://mirror.mi.hdm-stuttgart.de/ubuntu/Vmware/Mibuntu">compressed image</link>. Apple users: Consider buying <productname xlink:href="https://www.vmware.com/products/fusion.html">VMWare Fusion</productname>.</para> </listitem> <listitem> <para>Available for free <productname xlink:href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</productname> desktop virtualization as <link xlink:href="https://mirror.mi.hdm-stuttgart.de/ubuntu/VirtualBox/Mibuntu">compressed image</link>.</para> </listitem> <listitem> <para>The beasts are quite big (~20 GB on disk, ~ 5GB compressed download)! <emphasis role="red">You may prefer a wired connection in favour of WiFi !</emphasis></para> </listitem> <listitem> <para>Alternative: Native or dual boot <orgname xlink:href="https://www.ubuntu.com">Ubuntu</orgname> <quote xlink:href="https://www.ubuntu.com/download/desktop">Desktop</quote> installation.</para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_virtualboxConfigScreenshots"> <title><trademark xlink:href="https://www.virtualbox.org">Virtualbox</trademark> settings</title> <mediaobject> <imageobject> <imagedata fileref="Fig/virtualboxConfig.multi.svg"/> </imageobject> </mediaobject> </figure> </section> <section xml:id="sd1_sect_usingTheExercises"> <title>Using the exercises</title> <figure xml:id="sd1_fig_exerciseList"> <title>Embedded exercises</title> <itemizedlist> <listitem> <para>Tight relationship to the E-examination.</para> </listitem> <listitem> <para>Complete <link xlink:href="apb.html">list of exercises on offer</link>.</para> </listitem> </itemizedlist> </figure> <figure xml:id="fose2017_assetNavigation"> <title>Using the exercises</title> <mediaobject> <imageobjectco> <areaspec otherunits="imagemap" units="other"> <area coords="120,39,502,184" linkends="sd1_callout_usingExercises-1" units="calspair" xml:id="sd1_callout_usingExercises-1-co"/> <area coords="120,196,349,237" linkends="sd1_callout_usingExercises-2" units="calspair" xml:id="sd1_callout_usingExercises-2-co"/> <area coords="101,246,225,282" linkends="sd1_callout_usingExercises-3" units="calspair" xml:id="sd1_callout_usingExercises-3-co"/> </areaspec> <imageobject> <imagedata fileref="Fig/exercise.svg.marked.svg"/> </imageobject> </imageobjectco> </mediaobject> <calloutlist role="slideExclude"> <callout arearefs="sd1_callout_usingExercises-1-co" xml:id="sd1_callout_usingExercises-1"> <para>The question to be answered. See <link linkend="sd1_qanda_hotelKeyCard">current example in real</link>.</para> </callout> <callout arearefs="sd1_callout_usingExercises-2-co" xml:id="sd1_callout_usingExercises-2"> <para>The corresponding solution: <emphasis role="red">Try hard yourself before even considering the easy way!</emphasis>.</para> <para>If you have no clue how to begin ask fellow course mates for hints.</para> </callout> <callout arearefs="sd1_callout_usingExercises-3-co" xml:id="sd1_callout_usingExercises-3"> <para>Create a comment in order to:</para> <itemizedlist> <listitem> <para>Find help among your colleagues, tutors, lecturer or technical assistant.</para> </listitem> <listitem> <para>Report issues concerning ill-defined questions, missing explanations or support, wrong or deficient answers and so on.</para> </listitem> </itemizedlist> </callout> </calloutlist> </figure> </section> <section xml:id="sd1CommonRessources"> <title>Common software development related resources</title> <figure xml:id="sd1_fig_text_HdM_mailserver"> <title><xref linkend="glo_HdM"/> mail server</title> <para>Either of:</para> <itemizedlist> <listitem> <para>Read your mails at <link xlink:href="https://ox.hdm-stuttgart.de">https://ox.hdm-stuttgart.de</link> regularly.</para> <blockquote> <para>or</para> </blockquote> </listitem> <listitem> <para>Activate mail forwarding from <link xlink:href="https://ox.hdm-stuttgart.de">https://ox.hdm-stuttgart.de</link> to your <quote>real</quote> email account.</para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_text_MiVpn"> <title>Configure <acronym>MI <xref linkend="glo_VPN"/></acronym> client access</title> <itemizedlist> <listitem> <para>External <link xlink:href="https://klausur.mi.hdm-stuttgart.de/ilias">MI E-examination system</link> access requires <xref linkend="glo_VPN"/>:</para> <itemizedlist> <listitem> <para>Past years' E-examinations.</para> </listitem> <listitem> <para>Your personal exam results.</para> </listitem> </itemizedlist> </listitem> <listitem> <para>OpenVPN <acronym>wiki</acronym> <link xlink:href="https://wiki.mi.hdm-stuttgart.de/doku.php?id=studium:infrastruktur:vpn">installation page</link> (Login required).</para> </listitem> <listitem> <para><filename>HdM_MI_stud.ovpn</filename> allows for using a maximum of MI services.</para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_text_MiCloud"> <title>MI Cloud server</title> <informaltable border="0"> <tr> <td valign="top"><itemizedlist> <listitem> <para><link xlink:href="https://cloud.mi.hdm-stuttgart.de">https://cloud.mi.hdm-stuttgart.de</link>.</para> </listitem> <listitem> <para>25 GB free disk space.</para> </listitem> <listitem> <para><link xlink:href="https://nextcloud.com/install/#install-clients">Desktop and mobile clients</link>.</para> </listitem> </itemizedlist></td> <td valign="top"><mediaobject> <imageobject> <imagedata fileref="Fig/miCloud.svg"/> </imageobject> </mediaobject></td> </tr> </informaltable> </figure> <figure xml:id="sd1_fig_text_Mi_E_examination"> <title>E-examination resources</title> <informaltable border="0"> <colgroup width="75%"/> <colgroup width="25%"/> <tr> <td valign="top"><itemizedlist> <listitem> <para>Examination server <link xlink:href="https://klausur.mi.hdm-stuttgart.de">https://klausur.mi.hdm-stuttgart.de</link> providing recent examinations. (External access requires <link linkend="sd1_fig_text_MiVpn">Mi VPN</link>.)</para> </listitem> <listitem> <para><link xlink:href="https://learn.mi.hdm-stuttgart.de/files/CheatSheet"><quote>Cheat sheet uploads</quote></link> of <filename>zip</filename> Archives to be used during the examination.</para> <caution> <para>Restrict yourself to standard formats e.g. <acronym>png</acronym>, <acronym>gif</acronym>, <acronym>pdf</acronym> rather than using proprietary stuff like <acronym>.doc</acronym>, <acronym>.docx</acronym>, <acronym>.rtf</acronym> possibly without tool support during the examination.</para> </caution> </listitem> <listitem> <para>Tests at <link xlink:href="https://learn.mi.hdm-stuttgart.de">https://learn.mi.hdm-stuttgart.de</link>.</para> </listitem> </itemizedlist></td> <td valign="top"><mediaobject> <imageobject> <imagedata fileref="Fig/exam.svg"/> </imageobject> </mediaobject></td> </tr> </informaltable> </figure> <figure xml:id="sd1_fig_text_MiFileserver"> <title>MI File server</title> <itemizedlist> <listitem> <para>Accessing your computer pool home directory.</para> </listitem> <listitem> <para>Windows share \\mi-ad1.srv.mi.hdm-stuttgart.de\xy123 or \\192.168.111.15\xy123.</para> </listitem> <listitem> <para>Requires <link linkend="sd1_fig_text_MiVpn">Mi VPN</link>.</para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_text_MiGitSvnServer"> <title>MI <xref linkend="glo_Soft_Git"/> versioning server</title> <itemizedlist> <listitem> <para><link xlink:href="https://gitlab.mi.hdm-stuttgart.de">https://gitlab.mi.hdm-stuttgart.de</link>.</para> </listitem> <listitem> <para>Collaborative software development.</para> </listitem> </itemizedlist> </figure> </section> <section xml:id="sd1SectTutorials"> <title>Coached exercises</title> <figure xml:id="sd1_fig_tuitions"> <title>Coached exercises</title> <itemizedlist> <listitem> <para>Tuesday and Wednesday 17:45-19:15.</para> </listitem> <listitem> <para>Alternating seminar and software exercises: <abbrev>E.g.</abbrev> seminar on Tuesdays, exercises on Wednesdays.</para> </listitem> <listitem> <para>Seminar groups of ~12 participants assigned to a tutor.</para> </listitem> </itemizedlist> </figure> <para>Each lecture participant may start a project to gain bonus points with respect to the final examination. This bonus may be added to your final examination's mark:</para> <figure xml:id="sd1_fig_points"> <title>Bonus points</title> <itemizedlist> <listitem> <para>Precondition: You must pass the examination based on its own score excluding bonus points.</para> </listitem> <listitem> <para>Examination: 90 points resulting in <quote>1,0</quote>, 45 points resulting in <quote>4.0</quote>.</para> </listitem> <listitem> <para>0-10 bonus points on top of examination score in case of reaching at least 45 examination points.</para> </listitem> <listitem> <para>Examples:</para> <itemizedlist> <listitem> <para>40 examination points: <quote>Failed</quote> regardless of any number of bonus points</para> </listitem> <listitem> <para>45 Examination points, 10 bonus points. Result: 55 points resulting in a 3.0 mark rather than 4.0.</para> </listitem> </itemizedlist> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_seminarRules"> <title>Seminar rules and bonus points</title> <itemizedlist> <listitem> <para>Join exactly one group at the <link xlink:href="https://learn.mi.hdm-stuttgart.de/ilias/goto.php?target=crs_2137&client_id=iliasclient1">MI E-learning system</link>.</para> </listitem> <listitem> <para>Bonus point requirements:</para> <orderedlist> <listitem> <para>80% participation rate of all weekly appointments</para> </listitem> <listitem> <para>Presenting <emphasis role="bold">at least three exercise</emphasis> solutions of <uri xlink:href="https://freedocs.mi.hdm-stuttgart.de/apb.html">https://freedocs.mi.hdm-stuttgart.de/apb.html</uri>.</para> </listitem> </orderedlist> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_seminarRulesExercises"> <title>Presenting exercise solutions</title> <itemizedlist> <listitem> <para>Give a brief account of the exercise in question.</para> </listitem> <listitem> <para>Explain your solution's concept and present your code.</para> </listitem> <listitem> <para>Explain possible problems / pitfalls.</para> </listitem> <listitem> <para>Ask your tutor for exercises to avoid thematic clashes</para> </listitem> </itemizedlist> </figure> </section> <section xml:id="sd1SectToolsOfTheTrade1"> <title>Using plain <xref linkend="glo_Java"/></title> <para>We outline the process of:</para> <orderedlist> <listitem> <para>Editing source code.</para> </listitem> <listitem> <para>Compile <xref linkend="glo_Java"/> source code thereby generating so called class files.</para> </listitem> <listitem> <para>Executing class files by virtue of a piece of software being called the <quote xlink:href="https://www.javaworld.com/article/3272244/core-java/what-is-the-jvm-introducing-the-java-virtual-machine.html">Java Virtual Machine</quote>.</para> <para>Main idea: <emphasis>Write once, run everywhere.</emphasis></para> </listitem> </orderedlist> <figure xml:id="sd1_fig_editCompileExecute"> <title>Edit - compile - execute</title> <mediaobject> <imageobject> <imagedata fileref="Fig/javaWorkingTerminal.multi.svg"/> </imageobject> </mediaobject> </figure> <para>We now examine these three steps in greater detail starting with editing source code:</para> <figure xml:id="sd1_fig_imageco_editJava"> <title>Editing <xref linkend="glo_Java"/> files</title> <mediaobject> <imageobjectco> <areaspec otherunits="imagemap" units="other"> <area coords="206,202,327,275" linkends="sd1_fig_imageco_editJava-1.2" units="calspair" xml:id="sd1_fig_imageco_editJava-1-co"/> <area coords="143,4,425,72" linkends="sd1_fig_imageco_editJava-2.2" units="calspair" xml:id="sd1_fig_imageco_editJava-2.2-co"/> </areaspec> <imageobject> <imagedata fileref="Fig/javaEdit.svg.marked.svg"/> </imageobject> </imageobjectco> </mediaobject> <calloutlist role="slideExclude"> <callout arearefs="sd1_fig_imageco_editJava-1-co" xml:id="sd1_fig_imageco_editJava-1.2"> <para>The process of creating a text file from scratch or editing an existing one is called editing. It requires a text editor like <command xlink:href="https://www.nano-editor.org">nano</command>, <command xlink:href="https://www.vim.org">vi(m)</command>, <command xlink:href="https://atom.io">atom</command> or similar.</para> <tip> <para>You should learn quickly using a text editor of your choice. A basic command is sufficient. Both <command xlink:href="https://www.nano-editor.org">nano</command> and <command xlink:href="https://www.vim.org">vi(m)</command> are good choices on Linux/UNIX systems due to their widespread distribution. Windows and Apple users have different choices as well.</para> </tip> </callout> <callout arearefs="sd1_fig_imageco_editJava-2.2-co" xml:id="sd1_fig_imageco_editJava-2.2"> <para>The resulting files are just plain text. Unlike office documents they won't contain any layout or formatting like <abbrev>e.g.</abbrev> different font sizes, background colours or similar.</para> </callout> </calloutlist> </figure> <para>We shed some light on <xref linkend="glo_Java"/> class definitions:</para> <figure xml:id="sd1_fig_listing_structureClassDefinition"> <title>Defining class <classname>HelloWorld</classname></title> <programlisting language="java">// Filename HelloWorld.java <co linkends="sd1_fig_listing_structureClassDefinition-1" xml:id="sd1_fig_listing_structureClassDefinition-1-co"/> public class HelloWorld <co linkends="sd1_fig_listing_structureClassDefinition-2" xml:id="sd1_fig_listing_structureClassDefinition-2-co"/> { public static void main(String[] args) <co linkends="sd1_fig_listing_structureClassDefinition-3" xml:id="sd1_fig_listing_structureClassDefinition-3-co"/> { System.out.println("Hello, world"); <co linkends="sd1_fig_listing_structureClassDefinition-4" xml:id="sd1_fig_listing_structureClassDefinition-4-co"/> } }</programlisting> <calloutlist role="slideExclude"> <callout arearefs="sd1_fig_listing_structureClassDefinition-1-co" xml:id="sd1_fig_listing_structureClassDefinition-1"> <para>The text file <filename>HelloWorld.java</filename> contains a class definition. Classes are <xref linkend="glo_Java"/>'s main building blocks. Real world <xref linkend="glo_Java"/> applications consist of up to thousands of interacting classes.</para> </callout> <callout arearefs="sd1_fig_listing_structureClassDefinition-2-co" xml:id="sd1_fig_listing_structureClassDefinition-2"> <para><code language="java">... public class HelloWorld ...</code> actually defines a so called <xref linkend="glo_Java"/> class of name HelloWorld. We will refer to it as <quote>class HelloWorld</quote> for short. You'll learn about classes starting from <xref linkend="sw1ChapterObjectsClasses"/>. The following restrictions apply:</para> <orderedlist> <listitem> <para>Class names must obey <xref linkend="glo_Java"/> rules for so called identifiers. Spaces, operators like <quote>+</quote> and a lot of other characters are disallowed. More details are to be found in <xref linkend="sd1_fig_legalVariableName"/>.</para> </listitem> <listitem> <para>A <xref linkend="glo_Java"/> file's base name must match the primary (the one being defined using the <code language="java">public</code> modifier) class name it contains. In the given example the file's base name is <classname>HelloWorld</classname>. Defining <abbrev>e.g.</abbrev> <code language="java">... public class Hello {...</code> inside a file <filename>HelloWorld.java</filename> would be flagged as a compile time error.</para> </listitem> </orderedlist> </callout> <callout arearefs="sd1_fig_listing_structureClassDefinition-3-co" xml:id="sd1_fig_listing_structureClassDefinition-3"> <para><code language="java">public static void main(String[] args)</code> is a so called <link linkend="sd1SectClassMembers">class method</link> definition.</para> <para>For the time being we present a rule of thumb: Every <xref linkend="glo_Java"/> program to become executable requires at least one class containing (nearly) exactly the following<code language="java"> main(..)</code> method definition:</para> <programlisting language="java">public static void main(String[] args)</programlisting> </callout> <callout arearefs="sd1_fig_listing_structureClassDefinition-4-co" xml:id="sd1_fig_listing_structureClassDefinition-4"> <para>A <link linkend="sd1_chap_Statements">statement</link> example. This is our toy example's actual <quote>payload</quote>: Only this line of code has an observable effect on execution. All surrounding text is just boilerplate code being required to keep the <xref linkend="glo_Java"/> environment happy.</para> </callout> </calloutlist> </figure> <para>Prior to execution we require a compilation step:</para> <figure xml:id="sd1_fig_imageco_compileJava"> <title>Compiling <xref linkend="glo_Java"/> file</title> <mediaobject> <imageobjectco> <areaspec otherunits="imagemap" units="other"> <area coords="113,61,273,111" linkends="sd1_fig_imageco_compileJava-sourcecode" xml:id="sd1_fig_compileJava-sourcecode"> <alt>Sourcecode</alt> </area> <area coords="347,134,439,182" linkends="sd1_fig_imageco_compileJava-compiler" xml:id="sd1_fig_compileJava-compiler"> <alt>Java compiler</alt> </area> <area coords="481,129,641,178" linkends="sd1_fig_imageco_compileJava-bytecodeFile" xml:id="sd1_fig_compileJava-bytecodeFile"> <alt>Executable bytecode file</alt> </area> </areaspec> <imageobject> <imagedata fileref="Fig/javaCompile.svg.marked.svg"/> </imageobject> </imageobjectco> </mediaobject> <calloutlist role="slideExclude"> <callout arearefs="sd1_fig_compileJava-sourcecode" xml:id="sd1_fig_imageco_compileJava-sourcecode"> <para>The text file <filename>HelloWorld.java</filename> containing our <classname>HelloWorld</classname> class.</para> </callout> <callout arearefs="sd1_fig_compileJava-compiler" xml:id="sd1_fig_imageco_compileJava-compiler"> <para>The <xref linkend="glo_Java"/> compiler is a piece of software belonging to a <xref linkend="glo_JDK"/>. It will read the input filename <filename>HelloWorld.java</filename> and create a resulting output filename <filename>HelloWorld.class</filename>.</para> </callout> <callout arearefs="sd1_fig_compileJava-bytecodeFile" xml:id="sd1_fig_imageco_compileJava-bytecodeFile"> <para>A <xref linkend="glo_Java"/> bytecode file contains instructions to be interpreted by a <xref linkend="glo_Java"/> run time system (<xref linkend="glo_JRE"/>), see next step. Bytecode files are not meant to be viewed or edited.</para> <para>The main advantage of bytecode files is becoming independent of operating systems. Thus a given <classname>HelloWorld.class</classname> file may be executed on Linux, Windows, Mac-OS or any other <xref linkend="glo_JRE"/> providing system in exactly the same way.</para> </callout> </calloutlist> </figure> <figure xml:id="sd1_fig_imageco_compileJavaShell"> <title>Command line <xref linkend="glo_Java"/> file compilation</title> <screen>~/tmp$ ls -al <emphasis role="red">HelloWorld.class</emphasis> ls: cannot access '<emphasis role="red">HelloWorld.class</emphasis>': No such file or directory ~/tmp$ <emphasis role="red">javac</emphasis> HelloWorld.java ~/tmp$ ls -al <emphasis role="red">HelloWorld.class</emphasis> -rw-r--r-- 1 goik fb1prof 419 Sep 23 15:44 <emphasis role="red">HelloWorld.class</emphasis></screen> </figure> <para>Besides human readable portions bytecode files mainly contain binary data providing instructions to be executed:</para> <figure xml:id="sd1_fig_listing_javaBytecodeFile"> <title>Java byte code file <filename>HelloWorld.class</filename></title> <screen>Êþº¾^@^@^@6^@^] ^@^F^@^O ^@^P^@^Q^H^@^R ^@^S^@^T^G^@^U^G^@^V^A^@^F<init>^A^@^C()V^A^@^DCode^A^@^OLineNumberTable^A^@^Dmain^A^@^V(\ [Ljava/lang/String;)V^A^@ <emphasis role="red">SourceFile</emphasis>^A^@^O<emphasis role="red">HelloWorld.java</emphasis>^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^L<emphasis role="red">Hello, world</emphasis>^G^@^Z^L^@^[^@\ ^\^A^@ <emphasis role="red">HelloWorld</emphasis>^A^@^Pjava/lang/Object^A^@^Pjava/lang/System^A^@^Cout^A^@^ULjava/io/PrintStream;\ ^A^@^Sjava/io/PrintStream^A^@^G<emphasis role="red">println</emphasis>^A^@^U(Ljava/lang/String;)V^@!^@^E^@^F^@^@^@^@^@^\ B^@^A^@^G^@^H^@^A^@ ^@^@^@^]^@^A^@^A^@^@^@^E*·^@^A±^@^@^@^A^@ ^@^@^@^F^@^A^@^@^@^B^@ ^@^K^@^L^@^A^@ ^@^@^@%^@^B^@^A^@^@^@ ²^@^B^R^C¶^@^D±^@^@^@^A^@ ^@^@^@ ^@^B^@^@^@^D^@^H^@^E^@^A^@^M^@^@^@^B^@^N</screen> </figure> <figure xml:id="sd1_fig_listing_javaSourceVsBytecode"> <title>Source code <abbrev>vs.</abbrev> bytecode</title> <informaltable border="1"> <tr> <th><filename>HelloWorld.java</filename></th> <th><filename>HelloWorld.class</filename></th> </tr> <tr> <td valign="top"><itemizedlist> <listitem> <para>Human readable (kind of 😆).</para> </listitem> <listitem> <para>High abstraction level.</para> </listitem> <listitem> <para>Text file</para> </listitem> </itemizedlist></td> <td valign="top"><itemizedlist> <listitem> <para>Machine readable instructions.</para> </listitem> <listitem> <para>Non-editable (usually).</para> </listitem> <listitem> <para>Binary file.</para> </listitem> </itemizedlist></td> </tr> </informaltable> </figure> <figure xml:id="sd1_fig_imageco_executeJava"> <title>Executing byte code file <filename>HelloWorld.class</filename></title> <mediaobject> <imageobjectco> <areaspec otherunits="imagemap" units="other"> <area coords="244,4,499,101" linkends="sd1_fig_imageco_executeJava-1" units="calspair" xml:id="__area-5fxifu"/> <area coords="57,113,295,272" linkends="sd1_fig_imageco_executeJava-2" units="calspair" xml:id="__area-6imq1s"/> <area coords="306,110,501,278" linkends="sd1_fig_imageco_executeJava-3" units="calspair" xml:id="__area-ni0p8r"/> </areaspec> <imageobject> <imagedata fileref="Fig/javaExecute.svg.marked.svg"/> </imageobject> </imageobjectco> </mediaobject> <calloutlist role="slideExclude"> <callout arearefs="__area-5fxifu" xml:id="sd1_fig_imageco_executeJava-1"> <para>Passing a bytecode file to the Java Runtime System (<xref linkend="glo_JRE"/>):</para> <screen>java HelloWorld</screen> <caution> <para>The <filename>.class</filename> extension must be omitted! Trying <command xlink:href="https://docs.oracle.com/javase/10/tools/java.htm">java</command> <option>HelloWorld.class</option> will yield an error.</para> </caution> </callout> <callout arearefs="__area-6imq1s" xml:id="sd1_fig_imageco_executeJava-2"> <para>The bytecode input will pass various steps inside the <xref linkend="glo_JRE"/>. On successful processing the underlying operating system will eventually execute the desired actions on behalf of the <xref linkend="glo_JRE"/>.</para> </callout> <callout arearefs="__area-ni0p8r" xml:id="sd1_fig_imageco_executeJava-3"> <para>Terminal and keyboard are being linked to the operating system by two pre-connected communication channels called <quote>standard input</quote> and <quote>standard output</quote> respectively. In our toy example <classname>System.out</classname> represents the <quote>standard output</quote> channel and thus allows for execution of <code language="java">println("Hello, world")</code>.</para> </callout> </calloutlist> </figure> <figure xml:id="sd1_fig_imageco_executeJavaScreen"> <title>Command line byte code file <filename>HelloWorld.class</filename> execution</title> <screen>> java HelloWorld Hello, world</screen> <para>Remark: This executes <filename>HelloWorld.class</filename> rather than <filename>HelloWorld.java</filename>.</para> </figure> <figure xml:id="sd1_fig_imageco_installJdk"> <title><xref linkend="glo_JDK"/> installation options</title> <itemizedlist> <listitem> <para><link xlink:href="https://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html">Manual installation</link>.</para> </listitem> <listitem> <para>Linux <productname>Debian</productname> / <productname>Ubuntu 22.04:</productname></para> <screen>apt install openjdk-17-jdk</screen> </listitem> </itemizedlist> </figure> </section> <section xml:id="sd1CodeSnippets"> <title>Play!</title> <para>The following code snippets may be executed and modified without prior knowledge. They are intended to give you an idea about programming without exactly understanding whats going on. Just enjoy! We'll present the gory details later ...</para> <section xml:id="sd1SectSnippetHelloWorld"> <title><quote>Hello, World</quote> and friends.</title> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_SnippetHelloWorld"> <title>Extending class <classname>HelloWorld</classname></title> <qandadiv> <qandaentry> <question> <orderedlist> <listitem> <para>Create a class <classname>HelloWorld</classname> by using your favourite text editor ( <command xlink:href="https://www.nano-editor.org">nano</command>, <command xlink:href="https://www.vim.org">vim</command>, <command xlink:href="https://atom.io">atom</command> ...) inserting the following code into a file <filename>HelloWorld.java</filename>:</para> <programlisting language="java">public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World ..."); } }</programlisting> </listitem> <listitem> <para>Compile <filename>HelloWorld.java</filename>:</para> <programlisting language="none">javac HelloWorld.java</programlisting> <para>This should result in a newly generated bytecode file <filename>HelloWorld.class</filename>:</para> <screen>ls <emphasis role="red">HelloWorld.class</emphasis> HelloWorld.java</screen> </listitem> <listitem> <para>Execute <filename>HelloWorld.class</filename> by:</para> <programlisting language="none">java HelloWorld</programlisting> <warning> <para>Executing <code>java HelloWorld.class</code> will not work.</para> </warning> <para>You should see the following output in your terminal:</para> <screen>Hello, World ...</screen> <para>Extend the above code to produce a whole paragraph of sentences rather than just one single line. Your output might read:</para> <screen>Those who can do. Those, who cannot, teach. Those, who cannot teach, lecture trainee teachers teaching methodology.</screen> </listitem> </orderedlist> </question> <answer> <para>After creating a file <filename>HelloWorld.java</filename> we execute the compilation step:</para> <screen>>javac HelloWorld.java <co linkends="sd1QandaSnippetHelloWorldCompileStep-1" xml:id="sd1QandaSnippetHelloWorldCompileStep-1-co"/> > >ls -l <co linkends="sd1QandaSnippetHelloWorldCompileStep-2" xml:id="sd1QandaSnippetHelloWorldCompileStep-2-co"/> -rw-r--r-- 1 goik fb1prof 426 <emphasis role="red">Sep 25 09:14</emphasis> HelloWorld.class <co linkends="sd1QandaSnippetHelloWorldCompileStep-3" xml:id="sd1QandaSnippetHelloWorldCompileStep-3-co"/> -rw-r--r-- 1 goik fb1prof 124 <emphasis role="red">Sep 23 17:44</emphasis> HelloWorld.java <co linkends="sd1QandaSnippetHelloWorldCompileStep-4" xml:id="sd1QandaSnippetHelloWorldCompileStep-4-co"/></screen> <calloutlist> <callout arearefs="sd1QandaSnippetHelloWorldCompileStep-1-co" xml:id="sd1QandaSnippetHelloWorldCompileStep-1"> <para>The <command xlink:href="https://docs.oracle.com/javase/10/tools/javac.htm">javac</command> command will read <filename>HelloWorld.java</filename>. If no errors are found a <filename>HelloWorld.class</filename> bytecode file is being generated.</para> </callout> <callout arearefs="sd1QandaSnippetHelloWorldCompileStep-2-co" xml:id="sd1QandaSnippetHelloWorldCompileStep-2"> <para>The <command xlink:href="https://linux.die.net/man/1/ls">ls</command> command lists all files and directories being found in your current working directory. The <option>-l</option> option will create a longer listing providing privilege, user and group ownership, file size and modification time meta data.</para> </callout> <callout arearefs="sd1QandaSnippetHelloWorldCompileStep-3-co" xml:id="sd1QandaSnippetHelloWorldCompileStep-3"> <para>The bytecode file <filename>HelloWorld.class</filename> results from compilation in <coref linkend="sd1QandaSnippetHelloWorldCompileStep-1-co"/>.</para> </callout> <callout arearefs="sd1QandaSnippetHelloWorldCompileStep-4-co" xml:id="sd1QandaSnippetHelloWorldCompileStep-4"> <para>The source file containing our class definition code. Note <emphasis role="red">Sep 25 09:14</emphasis> being more recent than <emphasis role="red">Sep 23 17:44</emphasis>. This reflects the fact that compilation happened after editing <filename>HelloWorld.java</filename>.</para> </callout> </calloutlist> <para>Execution requires a <xref linkend="glo_Java"/> runtime by virtue of the <command xlink:href="https://docs.oracle.com/javase/10/tools/java.htm">java</command> command:</para> <informaltable border="0"> <tr> <td valign="top"><calloutlist> <callout arearefs="sd1QandaSnippetHelloWorldExecuteStep-1-co" xml:id="sd1QandaSnippetHelloWorldExecuteStep-1"> <para>Starting the <xref linkend="glo_Java"/> runtime. <option>HelloWorld</option> is a command line parameter to <command xlink:href="https://docs.oracle.com/javase/10/tools/java.htm">java</command>.</para> </callout> <callout arearefs="sd1QandaSnippetHelloWorldExecuteStep-2-co" xml:id="sd1QandaSnippetHelloWorldExecuteStep-2"> <para>The source line <code language="java">System.out.println("Hello, world")</code> causes the <xref linkend="glo_Java"/> runtime writing the string <code language="java">"Hello, world"</code> to the operating system's standard output channel appending a newline character.</para> </callout> </calloutlist></td> <td valign="top"><screen>>java HelloWorld <co linkends="sd1QandaSnippetHelloWorldExecuteStep-1" xml:id="sd1QandaSnippetHelloWorldExecuteStep-1-co"/> Hello, world <co linkends="sd1QandaSnippetHelloWorldExecuteStep-2" xml:id="sd1QandaSnippetHelloWorldExecuteStep-2-co"/></screen></td> </tr> </informaltable> <para>There are several options for creating a whole paragraph spanning multiple text lines:</para> <orderedlist> <listitem> <para>Using <link xlink:href="https://blogs.oracle.com/javamagazine/text-blocks-come-to-java">Java 13 multiline string literals</link> (easy):</para> <programlisting language="none">public static void main( String[] args ) { System.out.println(<emphasis role="red">"""</emphasis> Those who can do. Those, who cannot, teach. Those, who cannot teach, lecture trainee teachers teaching methodology.<emphasis role="red">"""</emphasis> ); }</programlisting> </listitem> <listitem> <para>Straightforward: Using multiple print statements:</para> <programlisting language="java">public class HelloWorld { public static void main(String[] args) { System.out.println("Those who can do."); System.out.println("Those, who cannot, teach."); System.out.println( "Those who cannot teach lecture trainee teachers teaching methodology."); } }</programlisting> </listitem> <listitem> <para>Long strings exceeding your editor's desired maximum line length may be decomposed into smaller chunks using the <quote>+</quote> <coref linkend="sd1QandaSnippetHelloWorldPlusOperator-1-co"/> string concatenation operator:</para> <programlisting language="java">public static void main(String[] args) { System.out.println("Those who can do."); System.out.println("Those, who cannot, teach."); System.out.println("Those, who cannot teach, " <emphasis role="red">+</emphasis> <co xml:id="sd1QandaSnippetHelloWorldPlusOperator-1-co"/> "lecture trainee teachers teaching methodology."); // continuation line }</programlisting> </listitem> <listitem> <para>The <methodname>System.out.println(...)</methodname> method adds a final line break to your output. The <xref linkend="glo_ASCII"/> newline character <quote><code language="java">\n</code></quote> <coref linkend="sd1QandaSnippetHelloWorldNewline-1-co"/> allows for using just a single <methodname xlink:href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/PrintStream.html#println(java.lang.String)">System.out.println("...")</methodname> statement:</para> <programlisting language="java">public static void main(String[] args) { System.out.println( "Those who can do.<emphasis role="red">\n</emphasis>" <co xml:id="sd1QandaSnippetHelloWorldNewline-1-co"/> + "Those, who cannot, teach.<emphasis role="red">\n</emphasis>" <coref linkend="sd1QandaSnippetHelloWorldNewline-1-co"/> + "Those, who cannot teach, " + "lecture trainee teachers teaching methodology."); // No <emphasis role="red">\n</emphasis> at end, print<emphasis role="red">ln</emphasis> does it. }</programlisting> </listitem> </orderedlist> </answer> </qandaentry> </qandadiv> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_renameClassFile"> <title>Renaming a class file</title> <qandadiv> <qandaentry> <question> <para>Consider HelloWorld.java again:</para> <informaltable border="0"> <tr> <td valign="top"><programlisting language="java">public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world"); } }</programlisting></td> <td valign="top"><orderedlist> <listitem> <para>Rename your existing HelloWorld.java file to Advance.java and try to compile it. What happens and why does it happen?</para> </listitem> <listitem> <para>How do you solve the problem by editing the file's content?</para> </listitem> </orderedlist></td> </tr> </informaltable> </question> <answer> <orderedlist> <listitem> <para>Renaming a <xref linkend="glo_Java"/> class file without renaming a contained <code language="java">public</code> class name results in a compile time error:</para> <screen>>mv HelloWorld.java Advance.java <co linkends="sd1_qanda_renameClassFile-1" xml:id="sd1_qanda_renameClassFile-1-co"/> > >javac Advance.java <co linkends="sd1_qanda_renameClassFile-2" xml:id="sd1_qanda_renameClassFile-2-co"/> Advance.java:2: error: class HelloWorld is public, should be declared in a file named HelloWorld.java public class HelloWorld { ^ <co linkends="sd1_qanda_renameClassFile-3" xml:id="sd1_qanda_renameClassFile-3-co"/> 1 error </screen> <calloutlist> <callout arearefs="sd1_qanda_renameClassFile-1-co" xml:id="sd1_qanda_renameClassFile-1"> <para>We rename <filename>HelloWorld.java</filename> to <filename>Advance.java</filename> on file system level without touching its content.</para> </callout> <callout arearefs="sd1_qanda_renameClassFile-2-co" xml:id="sd1_qanda_renameClassFile-2"> <para>Trying to compile <filename>Advance.java</filename>.</para> </callout> <callout arearefs="sd1_qanda_renameClassFile-3-co" xml:id="sd1_qanda_renameClassFile-3"> <para>Failure: The file <filename>Advance.java</filename> still contains a <code language="java">public class HelloWorld</code> definition. But <xref linkend="glo_Java"/> requires the class name being equal to the containing file's base name appending a <filename>.java</filename> extension.</para> <para>This error belongs to the wide class of compile time errors. More specific it is being called a syntax error violating <xref linkend="glo_Java"/> language constraints.</para> </callout> </calloutlist> </listitem> <listitem> <para>Compilation requires replacing the class name <classname>HelloWorld</classname> by <classname>Advance</classname>:</para> <informaltable border="0"> <tr> <td valign="top"><programlisting language="java">public class <emphasis role="red">Advance</emphasis> { public static void main(String[] args) { System.out.println("Hello, world"); } }</programlisting></td> <td valign="top"><screen>>javac Advance.java > >java Advance Hello, world</screen></td> </tr> </informaltable> </listitem> </orderedlist> </answer> </qandaentry> </qandadiv> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_compileTimeStamps"> <title>Editing and compilation</title> <qandadiv> <qandaentry> <question> <para>Consider the following class definition in a HelloWord file:</para> <programlisting language="java">public class HelloWorld { public static void main(String[] args) { System.out.println("<emphasis role="red">I have changed!</emphasis>"); } }</programlisting> <para>On execution a newbie programmer is stunned still seeing the former <code language="java">"Hello, world"</code> string:</para> <screen>>java HelloWorld <emphasis role="red">Hello, world</emphasis></screen> <para>The file system view reads:</para> <screen>>ls -l -rw-r--r-- 1 goik fb1prof 426 Sep 25 10:45 HelloWorld.class -rw-r--r-- 1 goik fb1prof 126 Sep 25 10:48 HelloWorld.java</screen> <para>Whats wrong here? Why does the "<emphasis role="red">I have changed!</emphasis>" string does not show up?</para> </question> <answer> <para>The file system reveals <filename>HelloWorld.class</filename> being about three minutes older than <filename>HelloWorld.java</filename>:</para> <screen>>ls -l -rw-r--r-- 1 goik fb1prof 426 Sep 25 <emphasis role="red">10:45</emphasis> HelloWorld.class -rw-r--r-- 1 goik fb1prof 126 Sep 25 <emphasis role="red">10:48</emphasis> HelloWorld.java</screen> <para>The programmer likely edited <filename>HelloWorld.java</filename> and forgot to invoke the compiler by executing <command>javac</command> <filename>HelloWorld.java</filename>. As a consequence the now outdated <filename>HelloWorld.class</filename> bytecode file was not replaced by a current version thus not reflecting the change of <filename>HelloWorld.java</filename> and thus became outdated.</para> <para>This is a common beginner's error: Integrated development environments like <xref linkend="glo_Soft_IntellijIDEA"/> and build tools like <xref linkend="glo_Maven"/> relieve you from the <quote>manually keeping track of dependencies</quote> burden: Editing a source code file will trigger compilation automatically. We'll <link linkend="sd1_sect_usingMavenIdea">turn to those higher level tools</link> after finishing our basic tour on <quote>pure</quote> <xref linkend="glo_Java"/> <command xlink:href="https://docs.oracle.com/javase/10/tools/javac.htm">javac</command> / <command xlink:href="https://docs.oracle.com/javase/10/tools/java.htm">java</command> commands and plain text source code editing.</para> </answer> </qandaentry> </qandadiv> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_editingBytecode"> <title>Editing bytecode</title> <qandadiv> <qandaentry> <question> <para>We reconsider the bytecode example from <xref linkend="sd1_fig_listing_javaBytecodeFile"/> being generated from a <filename>HelloWorld.java</filename> class file:</para> <screen>Êþº¾^@^@^@6^@^] ^@^F^@^O ^@^P^@^Q^H^@^R ^@^S^@^T^G^@^U^G^@^V^A^@^F<init>^A^@^C()V^A^@^DCode^A^@^OLineNumberTable^A^@^Dmain^A^@^V(\ [Ljava/lang/String;)V^A^@ SourceFile^A^@^OHelloWorld.java^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^L<emphasis role="red">Hello, world</emphasis>^G^@^Z^L^@^[^@\ ...</screen> <orderedlist> <listitem> <para>Use your favourite text editor for overwriting the string <code language="java">"Hello, world"</code> by <code language="java">"Software joy"</code> in <filename>HelloWorld.class</filename> (Not in <filename>HelloWorld.java</filename>!).</para> <screen>Êþº¾^@^@^@6^@^] ^@^F^@^O ^@^P^@^Q^H^@^R ^@^S^@^T^G^@^U^G^@^V^A^@^F<init>^A^@^C()V^A^@^DCode^A^@^OLineNumberTable^A^@^Dmain^A^@^V(\ [Ljava/lang/String;)V^A^@ SourceFile^A^@^OHelloWorld.java^L^@^G^@^H^G^@^W^L^@^X^@^Y^A^@^L<emphasis role="red">Software joy</emphasis>^G^@^Z^L^@^[^@\ ...</screen> <para>Try to execute this bytecode file (without recompiling). What do you observe?</para> </listitem> <listitem> <para>Recompile and repeat the process starting from a sound <filename>HelloWorld.class</filename> file. This time use a binary editor like <command xlink:href="https://linux.die.net/man/1/hexedit">hexedit</command> or <command xlink:href="https://manpages.debian.org/unstable/ghex/ghex.1.en.html">ghex</command> instead.</para> <tip> <para>Read your editor's documentation. Using <command xlink:href="https://linux.die.net/man/1/hexedit">hexedit</command> <option>HelloWorld.class</option> in a terminal just requires:</para> <itemizedlist> <listitem> <para>The <quote>tab</quote> key switching to the <xref linkend="glo_ASCII"/> section in the right pane.</para> </listitem> <listitem> <para><quote>Ctrl-X</quote> for leaving the editor and hitting the <quote>Y</quote> key to persist your changes.</para> </listitem> </itemizedlist> </tip> </listitem> <listitem> <para>Start again from a freshly compiled <filename>HelloWorld.class</filename>. Use a binary editor again to append an exclamation mark as in <code language="java">"Hello, world<emphasis role="red">!</emphasis>"</code>. The string's length will thus be increased by 1. What happens on execution?</para> </listitem> </orderedlist> </question> <answer> <orderedlist> <listitem> <para>Using a simple text editor doesn't work:</para> <screen>>java HelloWorld Error: LinkageError occurred while loading main class HelloWorld java.lang.ClassFormatError: Extra bytes at the end of class file HelloWorld </screen> <para>This error actually depends on your text editor choice: When using <command xlink:href="https://www.vim.org">vim</command> or <command xlink:href="https://www.nano-editor.org">nano</command> analyzing the modified bytecode file's content reveals the culprit at the very end:</para> <screen>... 00000090 17 0C 00 18 00 19 01 00 0C 43 65 6C 6C 6F 2C 20 .........<emphasis role="red">Hello,</emphasis> 000000A0 77 6F 72 6C 64 07 00 1A 0C 00 1B 00 1C 01 00 0A <emphasis role="red">world</emphasis>........... ... 00000190 00 0A 00 00 00 0A 00 02 00 00 00 03 00 08 00 04 ................ 000001A0 00 01 00 0D 00 00 00 02 00 0E <emphasis role="red">0A</emphasis> <co linkends="sd1_qanda_editingBytecodeBytecodeNewline-1" xml:id="sd1_qanda_editingBytecodeBytecodeNewline-1-co"/> ...........</screen> <calloutlist> <callout arearefs="sd1_qanda_editingBytecodeBytecodeNewline-1-co" xml:id="sd1_qanda_editingBytecodeBytecodeNewline-1"> <para>The <emphasis role="red">0A</emphasis> (10 in hexadecimal notation) is being appended to the bytecode file both by <command xlink:href="https://www.vim.org">vim</command> and <command xlink:href="https://www.nano-editor.org">nano</command>. This is the <xref linkend="glo_ASCII"/> <link xlink:href="https://en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart">value corresponding to a linefeed character</link>. Both editors thus add an empty new line on saving thereby corrupting the bytecode file.</para> </callout> </calloutlist> <para>The <command xlink:href="https://linux.die.net/man/1/emacs">emacs</command> editor for example refrains from auto-adding newlines and doesn't cause this problem.</para> </listitem> <listitem> <para>In any case using a binary editor like <command xlink:href="https://linux.die.net/man/1/hexedit">hexedit</command> or <command xlink:href="https://www.systutorials.com/docs/linux/man/1-ghex">ghex</command> leads to success :</para> <screen> ... ... 00000070 ... 61 0C 00 07 rceFile...HelloWorld.java... 0000008C ... 79 07 00 1A .............<emphasis role="red">Software joy</emphasis>... 000000A8 ... 61 2F 6C 61 ........HelloWorld...java/la ... ...</screen> <informaltable border="0"> <tr> <td valign="top"><para>Executing <command xlink:href="https://docs.oracle.com/javase/10/tools/java.htm">java</command> <option>HelloWorld</option>:</para></td> <td valign="top"><screen>>java HelloWorld <emphasis role="red">Software joy</emphasis></screen></td> </tr> </informaltable> </listitem> <listitem> <para>Extending the string even by just one character corrupts the bytecode file:</para> <screen>>java HelloWorld Error: LinkageError occurred while loading main class HelloWorld java.lang.ClassFormatError: Unknown constant tag 33 in class file HelloWorld</screen> <para>Using <command xlink:href="https://linux.die.net/man/1/hexedit">hexedit</command> we find the following content within the unspoiled bytecode file:</para> <screen>00 08 07 00 17 0C 00 18 00 19 01 00 0C <emphasis role="red">48 65 6C 6C 6F 2C 20 77 6F 72 6C 64</emphasis> 07 00 1A <emphasis role="red">▲</emphasis> <emphasis role="red">▲</emphasis> <emphasis role="red">┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛</emphasis> <emphasis role="red">┃</emphasis> <emphasis role="red">▼</emphasis> will be overridden by .............<emphasis role="red">Hello, world</emphasis>... »!« or 21 respectively</screen> <para>Thus appending an exclamation mark will override the hexadecimal <code language="java">07</code> value immediately following the <code language="java">"Hello, world"</code> string with a new value of <code language="java">21 (<xref linkend="glo_ASCII"/> for <quote>!</quote>)</code>. Apparently this value serves some purpose with respect to the virtual machine and must therefore not be altered. This is the byte code verifier's responsibility as being outlined in <xref linkend="sd1_fig_imageco_executeJava"/>.</para> </listitem> </orderedlist> <para>Overall conclusion: Tinkering with bytecode files is a bad idea unless you have a good reason and know what you are doing.</para> </answer> </qandaentry> </qandadiv> </qandaset> </section> <section xml:id="sd1SectSimpleMath"> <title>Simple calculations</title> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_simpleMath"> <title>Working with variables</title> <qandadiv> <qandaentry> <question> <para>This exercise is about two <link linkend="sd1_sect_variables">variables</link> <code language="java">a</code> and <code language="java">b</code> representing values being subject to change. Embed the following snippet in a class and execute the beast:</para> <programlisting language="java" linenumbering="numbered">public static void main(String[] args) { int a = 4, b = 7; System.out.println("a=" + a); System.out.println("b=" + b); int sum = a + b; System.out.println("Sum: " + sum); }</programlisting> <orderedlist> <listitem> <para>Modify these values and code more sophisticated expressions.</para> </listitem> <listitem> <para>Replace all three <methodname>System.out.println(...)</methodname> statements into a single one:</para> <informaltable border="0"> <tr> <th>Source code</th> <th>Expected output</th> </tr> <tr> <td valign="top"><programlisting language="java" linenumbering="unnumbered">int a = 4, b = 7; System.out.println(...); // TODO</programlisting></td> <td valign="top"><screen>4+7=11</screen></td> </tr> </informaltable> <para>Your code is expected to produce correct output when changing the values of the two <link linkend="sd1_sect_variables">variables</link> <code language="java">a</code> and <code language="java">b</code>:</para> <informaltable border="0"> <tr> <th>Source code</th> <th>Expected output</th> </tr> <tr> <td valign="top"><programlisting language="java" linenumbering="unnumbered">int a = -4, // Changing just values b = 100; System.out.println(...); // Remains unchanged</programlisting></td> <td valign="top"><screen>-4+100=96</screen></td> </tr> </informaltable> <tip> <para><xref linkend="glo_Java"/> does not only provide an arithmetic <code language="java">+</code> operator: You have already seen <link linkend="sd1_qanda_SnippetHelloWorld">the <code language="java">+</code> operator connecting two strings</link>. <xref linkend="glo_Java"/> extends this concept of concatenation to non-string values as well.</para> </tip> </listitem> </orderedlist> </question> <answer> <para>A naive solution reads:</para> <programlisting language="java" linenumbering="unnumbered">int a = -4, b = 100; System.out.println(a + "+" + b + "=" + a + b);</programlisting> <para>This unfortunately does not work. In ordinary math <abbrev>e.g.</abbrev> <code language="java">2 + 3 * 4</code> is being evaluated as <code language="java">2 + (3 * 4)</code> rather than <code language="java">(2 + 3) * 4</code>. Likewise the above expression is <emphasis>fully</emphasis> equivalent to:</para> <programlisting language="java" linenumbering="unnumbered">System.out.println(((((a + "+") + b) + "=") + a) + b);</programlisting> <para>As we shall see in <xref linkend="sd1SectOperator"/> each of these constituents evaluates to a string thus producing:</para> <screen>-4+<emphasis role="red">100</emphasis>=-4<emphasis role="red">100</emphasis></screen> <para>A better solution thus forcing an arithmetic <quote>+</quote> operation requires a pair of braces grouping <code language="java">(a+b)</code>:</para> <informaltable border="0"> <tr> <th>Source code</th> <th>Output</th> </tr> <tr> <td valign="top"><programlisting language="java" linenumbering="unnumbered">int a = -4, b = 100; System.out.println(a + "+" + b+ "=" + (a + b));</programlisting></td> <td valign="top"><screen>-4+100=96</screen></td> </tr> </informaltable> <para>Reverting to the original variable values 4 and 7 still yields a correct output:</para> <informaltable border="0"> <tr> <th>Source code</th> <th>Output</th> </tr> <tr> <td valign="top"><programlisting language="java" linenumbering="unnumbered">int a = 4, b = 7; System.out.println(a + "+" + b+ "=" + (a + b));</programlisting></td> <td valign="top"><screen>4+7=11</screen></td> </tr> </informaltable> </answer> </qandaentry> </qandadiv> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_AssocCodeEquivalence"> <title>Code equivalence</title> <qandadiv> <qandaentry> <question> <para>The solution of <xref linkend="sd1_qanda_simpleMath"/> claims full source code equivalence between <code language="java">System.out.println(a + "+" + b + "=" + a + b)</code> and <code language="java">System.out.println(((((a + "+") + b) + "=") + a) + b)</code>. Is there a way to prove this statement?</para> <tip> <para>Think about the <xref linkend="glo_Java"/> compile and execution process. What exactly does equivalence refer to? You may also want to read the <command xlink:href="https://linux.die.net/man/1/diff">diff</command> command's documentation.</para> </tip> </question> <answer> <para>We start from the following two source code versions:</para> <informaltable border="0"> <tr> <th>Version 1</th> <th>Version 2</th> </tr> <tr> <td valign="top"><programlisting language="java" linenumbering="unnumbered">public class HelloWorld { public static void main(String[] args) { int a = 4, b = 7; System.out.println(a + "+" + b+ "=" + a + b); } }</programlisting></td> <td valign="top"><programlisting language="java" linenumbering="unnumbered">public class HelloWorld { public static void main(String[] args) { int a = 4, b = 7; System.out.println(((((a + "+") + b) + "=") + a) + b); } }</programlisting></td> </tr> </informaltable> <para>Source code equivalence means equivalence of executing the generated bytecode. The easiest case happens if compiling both versions yields identical bytecode. This is indeed true and can be proven by:</para> <orderedlist> <listitem> <para>Compile source code version 1 by executing <command xlink:href="https://docs.oracle.com/javase/10/tools/javac.htm">javac</command> <option>HelloWorld.java</option>.</para> </listitem> <listitem> <para>Rename the resulting bytecode file <filename>HelloWorld.class</filename> to <filename>HelloWorld.1.class</filename>.</para> </listitem> <listitem> <para>Repeat the previous steps for source code version 2 resulting in <filename>HelloWorld.2.class</filename>.</para> </listitem> <listitem> <para>Compare the two files <filename>HelloWorld.1.class</filename> and <filename>HelloWorld.2.class</filename>. Result:</para> <screen><command xlink:href="https://linux.die.net/man/1/diff">diff</command> HelloWorld.1.class HelloWorld.2.class</screen> </listitem> </orderedlist> <para>The <command xlink:href="https://linux.die.net/man/1/diff">diff</command> command returning no result states the the two files to be identical. Otherwise we would see e.g.:</para> <screen><command xlink:href="https://linux.die.net/man/1/diff">diff</command> HelloWorld.class HelloWorld.2.class Binary files HelloWorld.class and HelloWorld.2.class differ</screen> </answer> </qandaentry> </qandadiv> </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_sourceDiffButEquivalence"> <title>Different byte code, same execution results</title> <qandadiv> <qandaentry> <question> <para>Can you imagine two different byte code files being fully equivalent with respect to execution?</para> </question> <answer> <para>There are various scenario for this to happen:</para> <orderedlist> <listitem> <para>Two source code versions may already be equivalent albeit syntactically different. Consider the following example differing only by the definition order of two variables <code language="java">a</code> and <code language="java">b</code>:</para> <informaltable border="0"> <tr> <th>Version 1</th> <th>Version 2</th> </tr> <tr> <td valign="top"><programlisting language="java" linenumbering="unnumbered">public class HelloWorld { public static void main(String[] args) { int a = 4; int b = 7; System.out.println(a + b); } }</programlisting></td> <td valign="top"><programlisting language="java" linenumbering="unnumbered">public class HelloWorld { public static void main(String[] args) { int b = 7; int a = 4; System.out.println(a + b); } }</programlisting></td> </tr> </informaltable> <para>Obviously these two versions are equivalent with respect to execution. However the derived byte code differs:</para> <screen>>diff HelloWorld.class.1 HelloWorld.class.2 Binary files HelloWorld.class.1 and HelloWorld.class.2 differ</screen> </listitem> <listitem> <para>The <command xlink:href="https://docs.oracle.com/javase/10/tools/javac.htm">javac</command> compiler allows for additional options. You may add e.g. <command xlink:href="https://docs.oracle.com/javase/10/tools/javac.htm">javac</command> <option>-g</option> <option>HelloWorld.java</option>. This adds so called debugging information to the generated byte code causing the latter to differ.</para> </listitem> <listitem> <para><command xlink:href="https://docs.oracle.com/javase/10/tools/javac.htm">javac</command> compilers from different vendors (Oracle, IBM, OpenJDK) or versions (1.7, 1.8, 1.9, 10, 11 and so on) will most likely produce different byte code hopefully executing identically.</para> </listitem> </orderedlist> </answer> </qandaentry> </qandadiv> </qandaset> </section> <section xml:id="sd1SectSimpleMathConditional"> <title xml:id="sd1SnippetConditional">A conditional</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaSnippetConditional"> <qandadiv> <qandaentry> <question> <para>The following snippet only contains a <methodname>main()</methodname> method. Add a surrounding class and execute:</para> <programlisting language="java">public static void main(String[] args) { int a = 3, b = 87; if (a < b) { System.out.println("a is smaller than b"); } else if (b < a) { System.out.println("b is smaller than a"); } else { System.out.println("b equals a"); } }</programlisting> <para>Test different values for <code language="java">a</code> and <code language="java">b</code> reaching all three branches.</para> </question> </qandaentry> </qandadiv> </qandaset> </section> <section xml:id="sd1SectSimpleMathLoop"> <title xml:id="sd1SectSnippeLoop">A loop</title> <qandaset defaultlabel="qanda" xml:id="sd1QandaSimpleMathLoop"> <qandadiv> <qandaentry> <question> <para>Execute</para> <programlisting language="java">public static void main(String[] args) { int i = 0; while ( i < 5) { System.out.println("loop # " + i); i = i + 1; } }</programlisting> <para>Examine the result and implement the following modifications:</para> <orderedlist> <listitem> <para>Create five additional lines of output</para> </listitem> <listitem> <para>Enlarge the gap between two adjacent lines of output to a value of 3 retaining the same number of output lines:</para> <screen>loop # 0 loop # 3 loop # 6 loop # 9 loop # 12</screen> </listitem> </orderedlist> </question> <answer> <orderedlist> <listitem> <para>The number of output lines is being determined by the loop's termination condition <code language="java">i < 5</code>. Replacing this limit by <code language="java">while ( i < 10)</code> achieves the desired result.</para> </listitem> <listitem> <para>We may adjust two related parameters:</para> <informaltable border="0"> <tr> <td valign="top"><calloutlist> <callout arearefs="qandaPlayLoop-1-co" xml:id="qandaPlayLoop-1"> <para>Adjusting the limit to accommodate larger values.</para> </callout> <callout arearefs="qandaPlayLoop-2-co" xml:id="qandaPlayLoop-2"> <para>Raising the gap to a value of 3.</para> </callout> </calloutlist></td> <td valign="top"><programlisting language="java">while ( i < 15 <co linkends="qandaPlayLoop-1" xml:id="qandaPlayLoop-1-co"/>) { System.out.println("loop # " + i); i = i + 3 <co linkends="qandaPlayLoop-2" xml:id="qandaPlayLoop-2-co"/>; }</programlisting></td> </tr> </informaltable> <para>Alternatively we may as well modify the print statement:</para> <programlisting language="java">System.out.println("loop # " + <emphasis role="bold">i * 3</emphasis>);</programlisting> </listitem> </orderedlist> </answer> </qandaentry> </qandadiv> </qandaset> </section> </section> <section xml:id="sdi_sect_ideaBaseUsage"> <title>Using <xref linkend="glo_Soft_IntellijIDEA"/></title> <figure xml:id="sd1_fig_ideaDownload"> <title>Downloading IntelliJ idea</title> <itemizedlist> <listitem> <para><link xlink:href="https://www.jetbrains.com/help/idea/installing-and-launching.html">Installation notes</link>.</para> </listitem> <listitem> <para><link xlink:href="https://www.jetbrains.com/idea/download">https://www.jetbrains.com/idea/download</link></para> <para>Choose <quote>Ultimate</quote>.</para> </listitem> <listitem> <para>Ubuntu Linux users:</para> <para><command>snap</command> <option>install</option> <option>--classic</option> <option>intellij-idea-ultimate</option></para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_ideaUltimateLicenseTypes"> <title>Idea »Ultimate« license types</title> <itemizedlist> <listitem> <para>By HdM license server:</para> <para><uri><emphasis role="red">http</emphasis>://jetbrains.mi.hdm-stuttgart.de:11111</uri></para> </listitem> <listitem> <para>Activation code for offline usage, apply at:</para> <para><uri xlink:href="https://www.jetbrains.com/shop/eform/students">www.jetbrains.com/shop/eform/students</uri></para> </listitem> <listitem> <para>See <link xlink:href="https://wiki.mi.hdm-stuttgart.de/doku.php?id=studium:infrastruktur:software:intellijidea&s[]=jetbrains"> MI wiki</link> for further details.</para> </listitem> </itemizedlist> </figure> <figure xml:id="sd1_fig_ideaUltimateHdmLicenseServer"> <title>Alternative: Using the HdM license server</title> <informaltable border="0"> <colgroup width="50%"/> <colgroup width="50%"/> <tr> <td valign="top"><mediaobject> <imageobject> <imagedata fileref="Fig/intelliJIDEALicenseActivation.png" scale="80"/> </imageobject> </mediaobject></td> <td valign="top"><para>Troubleshooting: Insert address manually:</para><para><uri>http://jetbrains.mi.hdm-stuttgart.de:11111</uri></para><para>External usage requires <link linkend="sd1_fig_text_MiVpn">VPN</link> !</para></td> </tr> </informaltable> </figure> <figure xml:id="sd1_fig_ideaNewJavaProject"> <title>Creating a new Java project</title> <mediaobject> <imageobject> <imagedata fileref="Fig/ideaNewJavaProject.multi.svg" scale="80"/> </imageobject> </mediaobject> </figure> <qandaset defaultlabel="qanda" xml:id="sd1_qanda_SnippetHelloWorldIdea"> <title>Getting used to <xref linkend="glo_Soft_IntellijIDEA"/></title> <qandadiv> <qandaentry> <question> <para>This exercise is about getting used to <xref linkend="glo_Soft_IntellijIDEA"/> in favour of using the command line. Repeat at least two of your exercises starting from <xref linkend="sd1_qanda_SnippetHelloWorld"/>.</para> </question> </qandaentry> </qandadiv> </qandaset> </section> </chapter>