<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coder-Blog &#187; Tutorials</title>
	<atom:link href="http://coder-blog.de/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder-blog.de</link>
	<description>Computer, Informatik, Internet und Programmieren</description>
	<lastBuildDate>Wed, 23 Sep 2009 09:25:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java-Tutorial: CowSay (Kuh lernt sprechen)</title>
		<link>http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen/</link>
		<comments>http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 21:29:27 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[bedingte-anweisungen]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[cowsay]]></category>
		<category><![CDATA[for-schleife]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=78</guid>
		<description><![CDATA[Dieses Java-Tutorial beschreibt wie wir unser Java cowsay mit mehr funktionalität ausstatten, sodass unsere Kuh nun sprechen lernt]]></description>
			<content:encoded><![CDATA[<p>Lange versprochen wenden wir uns jetzt unserer Kuh aus dem zweiten <strong>Java-Tutorial</strong> zu. Schauen wir uns noch einmal an wie das Unix-Programm <strong>cowsay</strong> funktioniert.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">cowsay <span style="color: #ff0000;">'Java rocks!'</span></pre></div></div>

<p>Die Ausgabe dazu sollte so aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> _____________
<span style="color: #000000; font-weight: bold;">&lt;</span> Java rocks<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000; font-weight: bold;">&gt;</span>
 <span style="color: #660033;">-------------</span>
        \   ^__^
         \  <span style="color: #7a0874; font-weight: bold;">&#40;</span>oo<span style="color: #7a0874; font-weight: bold;">&#41;</span>\_______
            <span style="color: #7a0874; font-weight: bold;">&#40;</span>__<span style="color: #7a0874; font-weight: bold;">&#41;</span>\       <span style="color: #7a0874; font-weight: bold;">&#41;</span>\<span style="color: #000000; font-weight: bold;">/</span>\
                <span style="color: #000000; font-weight: bold;">||</span>----w <span style="color: #000000; font-weight: bold;">|</span>
                <span style="color: #000000; font-weight: bold;">||</span>     <span style="color: #000000; font-weight: bold;">||</span></pre></div></div>

<p>Diese Funktionalität möchten wir nun nachbauen, dass wir unser cowsay mit diesem Befehl ausführen können und das Ergebnis genauso aussehen soll wie das original cowsay aus Unix.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java cowsay <span style="color: #ff0000;">'Java rocks!'</span></pre></div></div>

<p>Der Vorteil dabei ist jedoch, dass wir dieses Programm auf Windows, Linux, Mac OSX und allen anderen Betriebssystemen nutzen können, die auch von dem <strong>Java Runtime Environment</strong> unterstützt werden.<span id="more-78"></span><br />
Okay, fangen wir dann mal an. Zunächst erstellen wir uns eine <strong>Klasse cowsay</strong>, die auch die <strong>main-Methode</strong> enthält:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Zunächst sollten wir klären wie wir es schaffen, dass der Text, den wir beim Aufruf des cowsay-Programms mitgeben auch in unseren <strong>Java-Code</strong> kommt. Dazu ist dieses String[] args da! Wie wir in den letzten <strong>Tutorials</strong> gelernt haben, handelt es sich hierbei um ein <strong>Array</strong> des Typs String. Darin finden wir die Argumente, die wir an unser Programm in der Konsole übergeben haben. Bei unserem cowsay brauchen wir lediglich auf args[0] zuzugreifen, denn wir übergeben nur ein Argument. Sollten wir jedoch mehrere Argumente übergeben wollen, so trennen wir diese durch ein Leerzeichen im Programmaufruf und finden diese in args[0] … args[n] wieder. Je nachdem wie viele davon wir übergeben hatten.<br />
Ein Problem auf das wir stoßen werden, ist dass wir die Fehlermeldung &#8220;<strong>ArrayIndexOutOfBoundException</strong>&#8221; erhalten, wenn wir beim Programmaufruf keine Nachricht übergeben. Dies werden wir mit einer <strong>bedingten Anweisung</strong> lösen und eine Standardnachricht angeben.<br />
Deshalb waren mir auch die letzten Tutorials so wichtig, bevor wir dieses hier bearbeiten konnten.<br />
Bauen wir uns dazu nun folgenden Code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich sage nix&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//Hier geht es noch weiter</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hier prüfen wir mit args.length == 0, ob das args Array einen Wert zugewiesen bekommen hat. Ist dies der Fall, so ist die Länge des Arrays größer 0! Sollte es nun so sein, dass keine Nachricht eingegeben wurde, definieren wir args als ein String-Array-Objekt mit einer Länge von eins. Nun speichern wir die Standardnachricht &#8220;Ich sage nix&#8221; in das Array an der Stelle 0.</p>
<p>Wenden wir uns jetzt der Sprechblase zu. Wir müssen dabei darauf achten, dass die Größe dieser abhängig von der Länge der eingegeben Nachricht ist. Dieses Problem werden wir mit einer Schleife lösen können. Dies zeigt uns das folgende Code-Fragment:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich sage nix&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> messageLength <span style="color: #339933;">=</span> message.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//Laenge der Nachricht</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*********************************************
    *           Sprechblase erzeugen
    *********************************************/</span>
    <span style="color: #003399;">String</span> top <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>                <span style="color: #666666; font-style: italic;">//Obere Linie der Sprechblase</span>
    <span style="color: #003399;">String</span> bottom <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">//Untere Linie der Sprechblase</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Linke und rechte Grenze mit Nachricht der Sprechblase</span>
    <span style="color: #003399;">String</span> contentAndBorders <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt; &quot;</span> <span style="color: #339933;">+</span> message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Gesamte Sprechblase</span>
    <span style="color: #003399;">String</span> speechBubble<span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #666666; font-style: italic;">//Obere + untere Linie erzeugen</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> messageLength <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      top <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">;</span>
      bottom <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    speechBubble <span style="color: #339933;">=</span> top <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> contentAndBorders <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> bottom <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Jetzt folgt noch die Kuh</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die ersten Zeilen sind die gleichen wie im obigen Code. Wir weisen der Variablen message den Inhalt des Arrays an der Stelle 0 zu, damit wir uns das Schreiben der eckigen Klammern sparen können und diese Variable einfach sprechender ist, als ein args[0].<br />
Die Variable messageLength bekommt die Länge unserer Nachricht zugewiesen, da wir diese für die Schleife brauchen werden. Als nächstes erzeugen wir uns die Variablen top und bottom, die die Zeichen für die obere bzw. untere Linie enthalten werden. Diese bekommen jeweils zunächst ein Leerzeichen zugewiesen. Damit stellen wir sicher, dass über / unter der öffnenden spitzen Klammer keine Striche stehen werden.<br />
Die Variable messageAndBorders enthält die Nachricht und den linken und rechten Begrenzer gefolgt von einem Leerzeichen, damit links und rechts von der Nachricht etwas Platz bleibt. &#8211; Sieht einfach besser aus. Dadurch müssen wir aber auch die <strong>Schleife</strong> um zwei Durchläufe erweitern, um dies auszugleichen. In dieser Schleife werden die Variablen top und bottom mit den entsprechenden Zeichen &#8220;_&#8221; und &#8220;-&#8221; gefüllt. Es werden so viele Zeichen erstellt wie die Nachricht – verlängert um zwei – lang ist.<br />
Die Variable speechBubble soll die gesamte Sprechblase enthalten und bekommt deshalb top, contentAndBorders und bottom – jeweils gefolgt von einer &#8220;newline&#8221; – zugewiesen.</p>
<p>Jetzt fehlt uns nur noch die Kuh. Eigentlich könnten wir einfach den Code aus dem <strong>CowSay-Tutorial</strong> verwenden, denn es ändert sich hierbei nichts. Um aber so viele System.out.println zu vermeiden, speichern wir die Kuh in die kuh Variable und erstellen uns eine Variable cowsay, die die Sprechblase und die Kuh enthält, die wir dann schließlich ausgeben werden.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> cowsay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>args.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      args <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Ich sage nix&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> messageLength <span style="color: #339933;">=</span> message.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">//Laenge der Nachricht</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/*********************************************
    *           Sprechblase erzeugen
    **********************************************/</span>
    <span style="color: #003399;">String</span> top <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>                <span style="color: #666666; font-style: italic;">//Obere Linie der Sprechblase</span>
    <span style="color: #003399;">String</span> bottom <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">//Untere Linie der Sprechblase</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Linke und rechte Grenze mit Nachricht der Sprechblase</span>
    <span style="color: #003399;">String</span> contentAndBorders <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt; &quot;</span> <span style="color: #339933;">+</span> message <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Gesamte Sprechblase</span>
    <span style="color: #003399;">String</span> speechBubble<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Die Kuh</span>
    <span style="color: #003399;">String</span> kuh<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Die gesamte Ausgabe</span>
    <span style="color: #003399;">String</span> cowsay<span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #666666; font-style: italic;">//Obere + untere Linie erzeugen</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> messageLength <span style="color: #339933;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      top <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;_&quot;</span><span style="color: #339933;">;</span>
      bottom <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    speechBubble <span style="color: #339933;">=</span> top <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> contentAndBorders <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> bottom <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//Die Kuh erzeugen</span>
    kuh <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;       <span style="color: #000099; font-weight: bold;">\\</span>   ^__^&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;        <span style="color: #000099; font-weight: bold;">\\</span>  (oo)<span style="color: #000099; font-weight: bold;">\\</span>_______&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;           (__)<span style="color: #000099; font-weight: bold;">\\</span>       )<span style="color: #000099; font-weight: bold;">\\</span>/<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;               ||----w |&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;               ||     ||&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    cowsay <span style="color: #339933;">=</span> speechBubble <span style="color: #339933;">+</span> kuh<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>cowsay<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Jetzt ist unsere Kuh eigentlich fertig und kann auch verwendet werden. Wir werden sie aber noch in späteren Tutorials umbauen. Freut euch schon mal drauf.<br />
Wer keine Lust hat sich den Programmcode zu kopieren, kann hier die <a title="cowsay.class herunterladen" href="http://coder-blog.de/wp-content/uploads/2009/04/cowsay.class" target="_self">cowsay.class</a> und <a title="cowsay.java herunterladen" href="http://coder-blog.de/wp-content/uploads/2009/04/cowsay.java" target="_self">cowsay.java</a> herunterladen. Die cowsay.class kann direkt ausgeführt werden. Die cowsay.java müsste erst noch durch den Java Compiler gejagt werden.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Arrays</title>
		<link>http://coder-blog.de/java-tutorial-arrays/</link>
		<comments>http://coder-blog.de/java-tutorial-arrays/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 15:29:40 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[erweiterte-for-Schleife]]></category>
		<category><![CDATA[for-schleife]]></category>
		<category><![CDATA[java-arrays]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[uni-informatik]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=133</guid>
		<description><![CDATA[Dieses Tutorial zeigt uns die Verwendung von Arrays in Java]]></description>
			<content:encoded><![CDATA[<p>Wenden wir uns in diesem Tutorial einmal den <strong>Arrays</strong> in Java zu. Ein Array – auch Feld und Reihung genannt &#8211;  kann man mit einem Setzkasten vergleichen, dessen Plätze von 0 bis 1 durchnummeriert sind. In diese Plätze können Werte gespeichert werden ähnlich wie bei einer Variablen. Ein Array enthält jedoch mehrere Werte, die über einen ganzzahligen Index angesprochen werden können.</p>
<p>Am besten verdeutlicht man sich das mit einem Bild:</p>
<div class="mceTemp">
<dl id="attachment_134" class="wp-caption alignnone" style="width: 299px;">
<dt class="wp-caption-dt"><img class="size-full wp-image-134" title="Java-Tutorial: Arrays" src="http://coder-blog.de/wp-content/uploads/2009/04/tut_array.gif" alt="Bildliche Darstellung eines Arrays" width="289" height="60" /></dt>
</dl>
</div>
<p>Hierbei werden die Werte 5, 2, 3, 4 an die Stellen von 0 bis 3 gespeichert. Bei späteren Berechnungen sollte man daran denken, dass ein Array mit dem Index 0 beginnt.</p>
<p>Überführen wir nun unser Bild in Java-Code, um die Verwendung von Arrays zu verdeutlichen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mit den eckigen Klammern nach dem Typ sagen wir, dass wir ein Array erzeugen möchten. Die eckigen Klammern hinter die Bezeichnung des Arrays zu setzen ist auch möglich. Mit dem Schlüsselwort &#8220;new&#8221; erzeugen wir ein neues <strong>Array-Objekt</strong>, das Integer-Werte enthält und 4 Felder groß ist. Dabei können in einem Array nur Werte gespeichert werden, die auch zu dem angegebenen Typ passen. Würden wir nun versuchen einen String in unser Array zu speichern, bekämen wir eine Fehlermeldung.<span id="more-133"></span><br />
Java bietet uns noch eine andere Möglichkeit Arrays zu erstellen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> zahlArray<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Damit haben wir den obigen Code auf eine Zeile reduziert. Beides kann jedoch nicht kombiniert werden, sodass etwas wie dies nicht möglich ist und uns unweigerlich einen <strong>Compilerfehler</strong> bringt:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
zahlArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Dies können wir jedoch mit einem kleinen Trick umgehen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlen <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
zahlArray <span style="color: #339933;">=</span> zahlen<span style="color: #339933;">;</span></pre></div></div>

<p>Sowohl ist dies auch möglich:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Das größte Speichern von Arrays bringt uns nichts, wenn wir nicht auch auf die Werte darin zugreifen können. Dazu schreiben wir einfach hinter den Namen des Arrays in eckige Klammern den Indexwert, in denen unser gewünschter Wert gespeichert worden ist.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zahlArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mit zahlArray[0] greifen wir auf den Wert im Array zu, der unter dem Index 0 abgelegt worden ist. Da die Indizes bei Arrays mit 0 beginnen, erhalten wir als Ausgabe unsere 5.<br />
Einige werden sich auch schon sicher Gedanken dazu machen wie man ein ganzes Array durchlaufen könnte, um auf alle Werte zugreifen zu können. Mit <strong>Array.length</strong> können wir bequem auf die Länge unseres Arrays zugreifen und somit mit Hilfe einer Schleife – zum Beispiel – alle Elemente ausgeben.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> zahlArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> zahlArray.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zahlArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hierbei erzeugen wir wie in den obigen Beispielen das Array zahlArray und befüllen es mit den entsprechenden Werten. Mit der <strong>for-Schleife</strong> ist es uns nun möglich auf die Indizes von 0 bis 3 (zahlArray.length – 1) zuzugreifen.<br />
Versuchen wir auf ein Element zuzugreifen, dass außerhalb des Arrays liegt – also wenn wir bei uns auf zahlArray[4] zugreifen würden – bekommen wir eine <strong>ArrayIndexOutOfBoundsException</strong> Fehlermeldung.</p>
<p>Wie in anderen Programmiersprachen ist es uns in Java auch möglich <strong>mehrdimensionale Arrays</strong> zu erstellen. Mehrdimensionale Arrays kann man sich als Arrays von Arrays vorstellen. Somit könnten wir beispielsweise Koordinaten speichern und auf diese zugreifen, oder uns eine Matrix simulieren.<br />
Dies erfolgt folgendermaßen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixEins <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Alternativen</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixZwei<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> matrixDrei<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Mit diesem Code haben wir uns so gesehen drei Matrizen erzeugt, die jeweils aus zwei Zeilen und drei Spalten bestehen. Nun kommen wir zur Verwendung eines dieser mehrdimensionalen Arrays:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixEins <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> matrixEins.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> row <span style="color: #339933;">=</span> matrixEins<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> row.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>row<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Zunächst erzeugen wir uns wie schon weiter oben beschrieben ein <strong>zweidimensionales Array</strong> und befüllen es mit den Werten 1 und 2. Danach durchlaufen wir dieses Array mit zwei ineinander <strong>geschachtelten for-Schleifen</strong>, um auch alle Elemente zu erwischen. Dazu müssen wir uns ein neues Array anlegen, dass die jeweilige Zeile enthält und durchlaufen dieses neue Array mit der zweiten for-Schleife, um uns die Werte auszugeben, die in der aktuellen Zeile stecken. Ist diese Schleife abgearbeitet, wird dem row-Array die zweite Zeile des matrixEins-Arrays zugewiesen und die innere Schleife wird noch einmal abgearbeitet. Als Ausgabe wollten wir etwas sehen wie:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">111</span>
<span style="color: #000000;">222</span></pre></div></div>

<p>Ein letztes Thema in diesem Tutorial ist die <strong>erweiterte for-Schleife</strong>, die von den Java-Entwicklern erstellt worden ist, um uns ein wenig Tipparbeit abzunehmen.<br />
Mit der erweiterten for-Schleife könnten wir unseren obigen Code in das umwandeln:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> matrixEins <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
matrixEins<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> row <span style="color: #339933;">:</span> matrixEins<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">:</span> row<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Vom Prinzip her funktioniert die erweiterte for-Schleife wie die normale for-Schleife, unterscheidet sich jedoch in der Syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> Typ Bezeichner <span style="color: #339933;">:</span> <span style="color: #003399;">Array</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
…
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die erweiterte for-Schleife wird solange durchlaufen, bis das ganze Array abgearbeitet ist. Dabei wird der Variablen vor dem Doppelpunkt immer der aktuelle Wert aus dem Array übergeben, den wir zum Beispiel ausgeben können. Ist der Code im Rumpf abgearbeitet, wird der Variablen der nächste Wert, der im Array steckt übergeben, der dann wieder verarbeitet werden kann.</p>
<p>Somit hätten wir das Kapitel der <strong>Arrays in Java</strong> bearbeitet und können nun unsere Kuh vom Anfang endlich zum sprechen bringen. Dies jedoch im nächsten Tutorium.</p>
<p>Ansonsten bei Fragen fragen.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Schleifen</title>
		<link>http://coder-blog.de/java-tutorial-schleifen/</link>
		<comments>http://coder-blog.de/java-tutorial-schleifen/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 20:57:35 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[do-while-schleife]]></category>
		<category><![CDATA[for-schleife]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[javakurs]]></category>
		<category><![CDATA[schleifen]]></category>
		<category><![CDATA[while-schleife]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=129</guid>
		<description><![CDATA[Dieses Java-Tutorial zeigt uns wie wir Schleifen dazu benutzen, um Code mehrmals auszufüheren.]]></description>
			<content:encoded><![CDATA[<p>In diesem <strong>Tutorial</strong> werden wir uns den <strong>Schleifen</strong> zuwenden. Um zu verdeutlichen was Schleifen sind, schauen wir und zunächst einmal diesen Code an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 0</span>
i<span style="color: #339933;">++;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 1</span>
i<span style="color: #339933;">++;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 2</span>
i<span style="color: #339933;">++;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Ausgabe: 3</span></pre></div></div>

<p>Wie wir sehen, erzeugen wir uns zunächst eine Variable, die einen Wert von 0 zugewiesen bekommt und dann der Wert der Variablen ausgegeben wird. Danach wird der Wert der Variablen um eins erhöht und es erfolgt wieder eine Ausgabe … usw. Nun, da Programmierer schreibfaul sind und dieser Code einfach zu umständlich erscheint, gibt es in vielen Programmiersprachen Schleifen, die dazu dienen einen bestimmten Code mehrmals auszuführen. Programmiersprachen, die keine Schleifen unterstützen, verwenden oft das Konzept der Rekursion &#8211; dies unterstützt Java auch.</p>
<p>Java kennt hierbei drei Arten von Schleifen:</p>
<ul>
<li><strong>while-Schleife</strong></li>
<li><strong> do-while-Schleife</strong></li>
<li><strong> for-Schleife</strong></li>
</ul>
<p>Schleifen bestehen immer aus einer <strong>Schleifenbedingung</strong> und dem <strong>Rumpf</strong>. In der Schleifenbedingung befindet sich ein boolescher Ausdruck, der der Schleife sagt wie lange der im Rumpf stehende Programmcode wiederholt werden soll.<span id="more-129"></span></p>
<p>Wenden wir uns zunächst der <strong>while-Schleife</strong> zu und betrachten dazu den oben abgewandelten Code in &#8220;Schleifen-Schreibweise&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Compilieren wir nun dieses Code-Fragment, so sollten wir das gleiche Ergebnis wie oben erhalten. Doch was passiert nun hier? Zunächst erstellen wir wie oben die Variable i und weisen ihr den Wert 0 zu. Danach sehen wir die <strong>while-Schleife</strong> in Gebrauch. In den runden Klammern finden wir die Bedingung, die der Schleife sagt, wie lange sie wiederholt werden soll – nämlich so lange wie unser i kleiner-gleich drei ist. Nun gelangen wir in den Rumpf der Schleife. Wie wir sehen findet dort lediglich die Ausgabe von i statt. Danach wird i um eins erhöht. Das Erhöhen der Variablen dürfen wir nicht vergessen, denn sonst bekämen wir eine <strong>Endlosschleife</strong>, die theoretisch unendlich lang ausgeführt wird. Naja, Theorie und Praxis sehen hierbei anders aus, denn entweder erkennt unser Betriebssystem, dass es sich hier um Code handelt, der nicht vorwärts kommt und bricht diesen ab, oder wir brechen ihn selber ab, weil es uns zu doof ist so lange zu warten, oder uns schmiert irgendwann das System ab und wir müssen den Rechner neu starten. <img src='http://coder-blog.de/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /><br />
Schauen wir uns nun den obigen Code umgewandelt in eine <strong>for-Schleife</strong> an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was macht nun dieses Stück Code? In der <strong>for-Schleife</strong> deklarieren wir die Variable in den runden Klammern und weisen ihr den Wert 0 zu. Nach dem Semikolon folgt dann die Bedingung, die die gleiche ist wie bei der while-Schleife. Ein Semikolon weiter schreiben wir das rein, was mit unserer Zählvariablen i gemacht werden soll – sie wird um eins erhöht.<br />
Die Syntax einer <strong>for-Schleife</strong> sieht folgendermaßen aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>Zählvariable<span style="color: #339933;">;</span> Bedingung<span style="color: #339933;">;</span> Erhöhung der Zählvariablen<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
Code im Rumpf
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Der Code im Rumpf der Schleife wird so lange wiederholt, bis i den Wert von drei übersteigt.</p>
<p>Jetzt tun uns noch die <strong>do-while-Schleife</strong> und haben dieses Kapitel auch hinter uns <img src='http://coder-blog.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Zunächst einmal wenden wir uns wieder einem Code-Beispiel zu:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Okay, die <strong>do-while-Schleife</strong> unterscheidet sich von der <strong>while-Schleife</strong> einmal, dass da das &#8220;do&#8221; vor dem &#8220;while&#8221; steht und dadurch, dass eine <strong>do-while-Schleife</strong> <strong>mindestens einmal</strong> ausgeführt wird. Haben wir also eine Bedingung, die von vornherein &#8220;false&#8221; ist, so findet bei der while- und der for-Schleife <strong>keine</strong> Ausführung des Codes im Rumpf statt &#8211; jedoch bei der <strong>do-while-Schleife</strong>.</p>
<p>Nun sollte sich die Frage stellen wann man welche Schleife verwenden sollte. Bei der <strong>do-while-Schleife</strong> sollte es klar sein, dass man diese nutzt, um Code mindestens einmal ausgeführt zu haben. Bei der for- und while-Schleife ist es Wurschd, denn vom Aufwand her sollten beide gleich sein.</p>
<p>Also, noch ein fröhliches Coden … =)</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-schleifen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Kommentare</title>
		<link>http://coder-blog.de/java-tutorial-kommentare/</link>
		<comments>http://coder-blog.de/java-tutorial-kommentare/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 23:00:06 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[Blockkommentar]]></category>
		<category><![CDATA[informatik]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[javadoc]]></category>
		<category><![CDATA[JavaDoc-Kommentar]]></category>
		<category><![CDATA[Kommentare]]></category>
		<category><![CDATA[oop programmierung]]></category>
		<category><![CDATA[programmiersprache java]]></category>
		<category><![CDATA[uni-informatik]]></category>
		<category><![CDATA[Zeilenkommentar]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=121</guid>
		<description><![CDATA[Java-Tutorial zu den verschiedenen Kommentaren, die in der Programmiersprache Java zur Verfügung stehen.]]></description>
			<content:encoded><![CDATA[<p>Als <strong>Kommentare</strong> bezeichnet man in Programmiersprachen besondere Code-Teile, die vom Compiler nicht in Maschinencode – in <strong>Java Bytecode</strong> – übersetzt werden, sondern nur den Lesern beziehungsweise den Programmierern dienen.<br />
Neben <strong>selbst sprechenden Namen</strong> für Variablen, Klassen und Methoden, sollte man Kommentare verwenden, um bestimmte Stücke des Quellcodes zu dokumentieren. Damit erleichtert man zum Einen anderen Entwicklern zu verstehen was ein Codefragment macht und zum anderen ist man dadurch in der Lage auch ein Programm zu verstehen, das man zum letzten mal vor einigen Monaten angesehen hat.<br />
Auch benutzt man Kommentare, um Anmerkungen &#8211; wie TODOs &#8211; festzuhalten, die einem während dem Programmieren auffallen.</p>
<p>Java bietet uns drei Möglichkeiten von Kommentaren:</p>
<ul>
<li> <strong>Zeilenkommentar</strong></li>
<li><strong> Blockkommentar</strong></li>
<li><strong> JavaDoc-Kommentar</strong></li>
</ul>
<p>Ein <strong>Zeilenkommentar</strong> gilt nur für eine Zeile und wird mit einem // eingeleitet. Möchte man Kommentare über mehrere Zeilen hinweg setzten, so bietet sich das <strong>Blockkommentar</strong> dafür an. Dieses wird mit einem /* eingeleitet und endet mit */.<br />
Das <strong>JavaDoc-Kommentar</strong> ist ein besonderes Blockkommentar, das zum Beispiel Beschreibungen von Funktionen  und / oder deren Parameter enthält. Beginnt man ein Kommentar mit /** und schließt es mit */ ab, handelt es sich um diesen Typ von Kommentar. Mit dem im JDK mitgelieferten Tool <strong>javadoc</strong> ist es möglich diese Kommentare zu einer <strong>API-Dokumentation</strong> zu generieren.<span id="more-121"></span></p>
<p>Mit Kommentaren können wir Code auch zum Testen und zur Fehlersuche auskommentieren, um diese vor dem Compiler zu verstecken.</p>
<p>Dieses Beispiel soll uns die Verwendung von Kommentaren in Java verdeutlichen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author eugen
 * @version 1.0
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> RasenMaehen
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*
  * Dieses Programm berechnet, ob man seinen Rasen mähen sollte
  */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main <span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args <span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//Maße in cm</span>
    <span style="color: #000066; font-weight: bold;">int</span> aktuelleLaenge <span style="color: #339933;">=</span> <span style="color: #cc66cc;">53</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> gewuenschteLaenge <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> maxUeberschuss <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Wenn Ueberschuss zu groß, muss gemaeht werden</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>aktuelleLaenge – gewuenschteLaenge<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> maxUeberschuss <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rasen muss gemaeht werden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Geht noch!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Bei dem ersten Kommentar handelt es sich um einen <strong>JavaDoc-Kommentar</strong>, der besondere Schlüsselwörter wie @author enthält, die vom <strong>javadoc-Tool</strong> ausgewertet werden können.<br />
Der zweite Kommentar auf den wir stoßen ist ein <strong>Blockkommentar</strong>, der sich über drei Zeilen hinzieht.<br />
Bei dem dritten und vierten Kommentar verwenden wir nun den <strong>Zeilenkommentar</strong>, der wie gesagt nur eine Zeile auskommentiert.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-kommentare/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Operatoren</title>
		<link>http://coder-blog.de/java-tutorial-operatoren/</link>
		<comments>http://coder-blog.de/java-tutorial-operatoren/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 10:12:10 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[bedingte-anweisungen]]></category>
		<category><![CDATA[datentypen]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[javakurs]]></category>
		<category><![CDATA[logische Operatoren]]></category>
		<category><![CDATA[operatoren]]></category>
		<category><![CDATA[Überladenes Plus für Strings]]></category>
		<category><![CDATA[uni-informatik]]></category>
		<category><![CDATA[Zuweisungsoperatoren]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=114</guid>
		<description><![CDATA[Dieses Java-Tutorial bringt uns die verschiedenen Operatoren, die in Java verwendet werden können näher]]></description>
			<content:encoded><![CDATA[<p>In den letzten <strong>Java-Tutorials</strong> haben wir fast instinktiv Operatoren genutzt und ich habe noch einige <strong>logische Operatoren</strong> erwähnt. In diesem Java-Tutorial möchten wir und nun näher mit Operatoren befassen.<br />
Betrachten wir uns dafür folgenden Programmcode:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> ersteVariable <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> zweiteVariable<span style="color: #339933;">;</span>
zweiteVariable <span style="color: #339933;">=</span> ersteVariable <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span></pre></div></div>

<p>Wir deklarieren uns die zwei Variablen ersteVariable und zweiteVariable. Der erstenVariable weisen wir den Wert 5 zu. Die zweiteVariable bekommt ein Ergebnis einer mathematischen Formel zugewiesen.<br />
Wo stehen nun die Operatoren? Der erste Operator, der zum Einsatz kommt, ist der <strong>Zuweisungsoperator</strong> =, der beispielsweise einer Variablen, die vor diesem Operator steht, einen bestimmten Wert, der dahinter kommt, zuweist.<br />
Einen weiteren Operator finden wir in der Multiplikation. Der <strong>* &#8211; Operator</strong> multipliziert zwei Werte miteinander. Die Variable ersteVariable und die Zahl 2 werden hierbei als <strong>Operanden</strong> bezeichnet.<span id="more-114"></span><br />
In Java findet man im Grunde drei Arten von Operatoren. So gibt es die <strong>unären Operatoren</strong>, die nur auf einen Operanden Anwendung finden. Der große Bruder davon ist der <strong>binäre Operator</strong>, oder auch <strong>zweistellige Operator</strong>. Diese sind beispielsweise Operatoren, die in der Mathematik gebraucht werden – also Plus, Minus, Mal und Geteilt. Wie man auch vermuten kann, kommt jetzt der <strong>dreistellige Operator.</strong> So gibt es auch den <strong>Fragezeichenoperator</strong> (?-Operator), der zur Fallunterscheidung genutzt werden kann:<br />
<code>Syntax: Bedingung ? Dann-Fall : Sonst-Fall</code><br />
Mit Operatoren ist es uns möglich einzelne Ausdrücke zu neuen Ausdrücken zu verbinden – wie in der Mathematik bei der Addition. Operatoren, die zur üblichen Rechnung genutzt werden, bezeichnet man auch als <strong>arithmetische Operatoren</strong> (+, -, *, /, %). Der <strong>%-Operator</strong>, oder auch <strong>Modulo-Operator</strong> / <strong>Restwert-Operator</strong> gibt uns den Rest einer Division zurück. So bekämen wir bei dem Ausdruck 5 % 3 als Ergebnis die Zahl 2, denn 5 / 3 = 1 Rest 2.</p>
<p>Weiter oben haben wir den Zuweisungsoperator = kennen gelernt. Schauen wir uns diesen Code an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ich habe Geburtstag!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
meinAlter <span style="color: #339933;">=</span> meinAlter <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mein neues Alter ist jetzt &quot;</span> <span style="color: #339933;">+</span> meinAlter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Da der Informatiker relativ schreibfaul ist, finden wir in vielen Programmiersprachen – so auch in Java – die <strong>Verbundoperatoren</strong>. Diese verkürzen Ausdrücke wie meinAlter = meinAlter + 1 auf meinAlter += 1. Dies gilt auch für die Subtraktion (-=), Multiplikation (*=) und Division (/=).<br />
In diesem Code hat sich aber auch noch ein weiterer Operator versteckt. Es ist das <strong>überladene Plus (+) für Strings</strong>. Damit ist es uns möglich etwas von beliebigem Typ an eine Zeichenkette zu hängen. In unserem Beispiel: im letzten Systen.out.println() wird die Zahl 55 aus meinAlter an den String &#8220;Mein neues Alter ist jetzt &#8221; gehängt.<br />
Zwei weitere Operatoren, die uns schreibfaule Informatiker unterstützen, sind die <strong>Präfix-Inkrement</strong> bzw. <strong>Postfix-Inkrement</strong> und <strong>Präfix-Dekrement</strong> bzw. <strong>Postfix-Dekrement</strong> Operatoren. Damit ist es uns möglich eine Zahl um eine Eins zu erhöhen bzw. zu verringern. Dazu wird beispielsweise vor oder hinter einer Variablen ein ++ oder ein &#8212; geschrieben.<br />
So hätten wir auch anstatt</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">meinAlter <span style="color: #339933;">=</span> meinAlter <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<p> einfach schreiben können</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">meinAlter<span style="color: #339933;">++</span></pre></div></div>

<p> Hätten wir zufällig einen Jungbrunnen entdeckt, könnten wir auch schreiben</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">meinAlter<span style="color: #339933;">--</span></pre></div></div>

<p>Aus der Mathematik kennen wir auch Operatoren, wie <strong>Kleiner</strong> (&lt;), <strong>Größer</strong> (&gt;), <strong>Kleiner-gleich</strong>(&lt;=), und <strong>Größer-gleich</strong> (&gt;=). Diese können wir in Java als <strong>Vergleichsoperatoren</strong> verwenden und haben es auch schon im <a title="Java-Tutorial: Fallunterscheidungen" href="http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/" target="_self">Java-Tutorial: Fallunterscheidungen</a> getan. So haben wir geschrieben:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
 
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>„Rentenalter nicht erreicht – weiterackern<span style="color: #339933;">!</span>“<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Hier haben wir verglichen, ob die Variable meinAlter kleiner ist als die rentenalter. Zwei weitere <strong>Vergleichsoperatoren</strong> sind == und !=. Damit ist es uns möglich zu überprüfen, ob zwei Werte gleich, oder ungleich sind. Alle Vergleichsoperatoren liefern uns als Ergebnis einen <strong>boolschen Wert</strong>, der entweder true oder false ist. So liefert uns beispielsweise meinAlter < rentenalter ein true, denn es ist wahr, dass meinAlter kleiner ist als das rentenalter. Schrieben wir meinAlter == rentenalter, bekämen wir ein false, denn meinAlter ist eben nicht gleich rentenalter. So liefert meinAlter != rentenalter wiederum true.<br />
Widmen wir uns jetzt aber den <strong>logischen Operatoren</strong> AND (&#038;&#038;), OR(||), XOR (^) und dem Nicht (!) zu. Mit diesen Operatoren ist es uns möglich verschiedene boolsche Ausdrücke zu verknüpfen. Zum Beispiel ist es uns jetzt möglich zu überprüfen, ob meinAlter < rentenalter und zugleich meinAlter > 17 ist. In Java-Code geschrieben sieht das wie folgt aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">meinAlter <span style="color: #339933;">&lt;</span> rentenalter <span style="color: #339933;">&amp;&amp;</span> meinAlter <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">17</span></pre></div></div>

<p>Der Ausdruck meinAlter < rentenalter liefert uns true, ebenso wie meinAlter > 17, somit ist der gesamte Ausdruck ebenfalls true, denn der <strong>AND-Operator</strong> liefert uns nur ein Wahr, wenn beide Ausdrücke, die miteinander verbunden werden auch wahr sind. Dagegen liefert uns der <strong>OR-Operator</strong> immer dann ein Wahr, wenn mindestens ein boolscher Ausdruck darin wahr ist, das uns folgender Code zeigt:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">meinAlter <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">17</span> <span style="color: #339933;">||</span> meinAlter <span style="color: #339933;">&lt;</span> rentenalter</pre></div></div>

<p>meinAlter < 17 ist falsch. Dagegen ist meinAlter < rentenalter wie bereits erwähnt wahr. Als Ergebnis liefert uns der <strong>||-Operator</strong> ein true.<br />
Möchte man dagegen das Oder aus dem üblichen Sprachgebrauch verwenden, das besagt, dass entweder dies oder das gilt, so wenden wir uns an den<strong> XOR-Operatoren</strong>. Der XOR-Operator liefert uns nur dann einen wahren Wert, wenn entweder der eine Ausdruck, oder der andere Ausdruck, die miteinander verknüpft werden, wahr sind.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">meinAlter <span style="color: #339933;">&lt;</span> rentenalter <span style="color: #339933;">^</span> meinAlter <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">17</span></pre></div></div>

<p>Dieser Ausdruck liefert uns false, denn sowohl meinAlter < rentenalter, als auch meinAlter > 17 sind wahr.<br />
Der letzte Operator, den wir in diesem <strong>Java-Tutorial</strong> behandeln werden ist der <strong>Nicht-Operator</strong>. Er dreht einfach einen boolschen Wert um. So wird aus einem Wahr ein Falsch und umgekehrt.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">17</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Bei diesem Code bekommen wir also ein true, denn meinAlter < 17 liefert uns ein Falsch. Dieses wird durch den <strong>Nicht-Operator</strong> zu einem Wahr.<br />
Damit haben wir das Kapitel Operatoren hinter uns gebracht und können uns anderen schönen Dingen in Java zuwenden.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-operatoren/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Fallunterscheidungen (Bedingte Anweisungen)</title>
		<link>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/</link>
		<comments>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 23:28:04 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[bedingte-anweisungen]]></category>
		<category><![CDATA[else-Anweisung]]></category>
		<category><![CDATA[else-if-anweisung]]></category>
		<category><![CDATA[fallunterscheidungen]]></category>
		<category><![CDATA[if-Anweisung]]></category>
		<category><![CDATA[java-arrays]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>
		<category><![CDATA[switch-anweisung]]></category>
		<category><![CDATA[variablen]]></category>
		<category><![CDATA[verschachtelte-fallunterscheidung]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=103</guid>
		<description><![CDATA[Dieses Java-Tutoral bringt uns Fallunterscheidungen oder auch bedingte Anweisungen näher]]></description>
			<content:encoded><![CDATA[<p>Wie in anderen Programmiersprachen, so bietet natürlich auch Java die Möglichkeit der <strong>Fallunterscheidung</strong> zu nutzen, die auch in mancher Literatur als <strong>bedingte Anweisung</strong> beschrieben wird.<br />
Ohne Fallunterscheidungen wäre ein Programm ziemlich doof, denn es könnte nicht flexibel Programmteile ausführen, die nur unter einer <strong>bestimmten Bedingung</strong> aufgerufen werden soll. Dazu bietet Java die <strong>if-</strong> bzw. <strong>if/else-Anweisungen</strong> und die <strong>switch-Anweisung</strong>.</p>
<p><strong>Bedingte Anweisungen</strong> sind gar nicht so schwer – also fangen wir gleich mit der <strong>if-Anweisung</strong> an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">!=</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was macht dieses Programm nun? Wir setzen uns zwei Variablen mit den Werten 55 und 66. Die Variable meinAlter soll das jetzige Alter repräsentieren. Die Zweite enthält das gegenwärtige Rentenalter 66. Darunter sehen wir die <strong>if-Anweisung</strong>, die – wie jede if-Anweisung – mit dem Schlüsselwort if eingeleitet wird und in Klammern einen <strong>boolschen Wert</strong> enthält.<br />
<span id="more-103"></span><br />
Hä, ich sehe aber gar keinen boolschen Wert?! Auf den ersten Blick erkennt man dies auch nicht sofort, doch der <strong>Operator !=</strong> vergleicht zwei Werte auf <strong>Nicht-Gleichheit</strong> und liefert uns einen boolschen Wert als Ergebnis. Als Gegenstück dazu gibt es den <strong>== Operator</strong>, der zwei <strong>Werte auf Gleichheit</strong> hin überprüft. In unserem Fall würde es bedeuten, dass wir den Wert der Variablen meinAlter mit dem Wert der Variablen rentenalter vergleichen. Wir fragen uns also: &#8220;Ist der Wert der Variablen meinAlter ungleich des Wertes der Variablen rentenalter?&#8221;. Als Ergebnis bekommen wir von Java ein true – also es ist wahr, dass beide ungleich sind.<br />
Da diese Bedingung nun wahr ist, wir der Code, der sich unterhalb der <strong>if-Anweisung</strong> in den geschweiften Klammern befindet ausgeführt.<br />
In normaler Sprache ausgedrückt, könnte man diese Bedingung auch schreiben als: &#8220;Wenn meinAlter ungleich rentenalter ist, dann gib mir aus &#8216;Rentenalter nicht erreicht – weiterackern!&#8217;&#8221;.</p>
<p>Natürlich ist diese Bedingung nicht gut programmiert, denn wenn meinAlter einen Wert enthält, der größer als 66 ist, wir die Meldung ebenfalls ausgegeben. Stattdessen sollten wir den <strong>< Operator</strong> benutzen, der wie in der Mathematik prüft, ob der erste Wert kleiner dem zweiten Wert ist.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So ist der Code schon viel besser. Was ist, wenn wir aber auch etwas ausführen möchten, wenn die if-Anweisung nicht zutrifft? Dafür gibt es eben die<strong> if-/else-Anweisung</strong>. Fügen wir nun unserem if- einen <strong>else-Zweig</strong> hinzu.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ins else wird immer nur dann gesprungen, wenn das if nicht zutrifft. Bei uns würde eben die if-Anweisung zuschlagen, da unsere meinAlter-Variable kleiner ist als die rentenalter.<br />
Ändern wir nun meinAlter zum Beispiel in 80, so würde die if-Anweisung nicht mehr zutreffen und dem Else-Zweig Beachtung geschenkt werden.</p>
<p>Es sind auch <strong>verschachtelte Fallunterscheidungen</strong> möglich:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">969</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Na, willst du Methusalem Konkurrenz machen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wir haben meinAlter auf 1000 geändert. Da jetzt die Bedingung der <strong>if-Anweisung</strong> nicht zutrifft, wird in die <strong>else-Anweisung</strong> gesprungen. Darin befindet sich nun ebenfalls eine <strong>if-/else-Anweisung</strong>. Innerhalb des else-Zweiges, wird jetzt im if geprüft, ob meinAlter größer oder gleich 969 ist. Dies trifft nun zu und es wir die entsprechende Nachricht ausgegeben.<br />
Für solch einen Fall bietet uns Java ein besonderes Konstrukt. Wir hätten also auch schreiben können:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">969</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Na, willst du Methusalem Konkurrenz machen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die <strong>else-if-Anweisung</strong> ist eine Kombination der <strong>if –</strong> und <strong>else-Anweisung</strong>. Denn trifft nun der <strong>if-Fall</strong> bei uns nicht zu, dann wird der <strong>else-if-Fall</strong> überprüft. Trifft dieser auch nicht zu, wird <strong>else</strong> ausgeführt. Als Anmerkung sei noch gesagt, dass es uns möglich ist, so viele else-if-Fälle zu benutzen wie wir wollen.</p>
<p>Neben der <strong>if-Anweisung</strong> und der <strong>if-/else-Anweisung</strong> findet man die <strong>switch-Anweisung</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>meinAlter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Na, heute Windeln schon gewechselt bekommen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Du darfst in Rente gehen!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">969</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Methusalem lässt grüßen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">4723</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Der Methuselah-Baum ist so alt wie du – Respekt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ja, schönes Alter!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Der <strong>switch-Anweisung</strong> wird zunächst ein Wert übergeben. Dieser wir dann überprüft, ob er sich einem bestimmten Fall (case) zuordnen lassen kann. Ist dies nicht möglich, wird der <strong>default-Fall</strong> angewandt. Der default-Fall ist optional und kann auch weggelassen werden. Bei switch ist zu beachten, dass die <strong>break-Anweisungen</strong> gesetzt sind. Ein Break bedeutet, dass nach Aufruf der break-Anweisung der weitere Durchlauf einer Anweisung abgebrochen wird. Wird ein Break bei der switch-Anweisung vergessen, so erzeugen wir einen <strong>Fall-Through</strong>. Es wird jetzt nicht abgebrochen sobald ein Fall eingetroffen ist, sondern es werden die weiter unten stehenden Fälle auch überprüft.</p>
<p>Jetzt müssen wir nur noch etwas über <strong>Arrays</strong> erfahren und wir können unser <strong>CowSay-Programm</strong> zum &#8220;Sprechen&#8221; bringen.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Datentypen</title>
		<link>http://coder-blog.de/java-tutorial-datentypen/</link>
		<comments>http://coder-blog.de/java-tutorial-datentypen/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 22:42:38 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[datentypen]]></category>
		<category><![CDATA[java tutorials]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[uni-informatik]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=92</guid>
		<description><![CDATA[In diesem Java-Tutorial erfahren wir was Datentypen sind und lernen einige davon kennen.]]></description>
			<content:encoded><![CDATA[<p>In diesem <strong>Java-Tutorial</strong> erfahren wir was <strong>Datentypen</strong> sind und lernen einige davon kennen.</p>
<p>&#8220;Aber was soll eigentlich dieses &#8216;int&#8217; da?&#8221; hat man sich im <strong><a title="Java-Tutorial: Variablen" href="http://coder-blog.de/java-tutorial-variablen/" target="_self">Java-Tutorial: Variablen</a></strong> gefragt. Dieses &#8220;int&#8221; gibt den Typ unserer Variable aus. Es sagt dem Compiler erstmal wieviel Speicherplatz er reservieren muss und zweitens welche Daten man in diese Variable Speichern kann.</p>
<p>Dies führt uns zum Thema der Datentypen, denn genau dieses &#8220;int&#8221; ist ein Schlüsselwort für den <strong>Datentyp Integer</strong> in Java. Hat man eine Variable als Integer deklariert, so kann man nur ganzzahlige Werte darin Speichern. Also alle Zahlen, die kein Komma haben &#8211; wie unsere 5 aus dem Beispiel. Würden wir dennoch zum Beispiel versuchen die 5,5 darin zu speichern, bekämen wir eine <strong>Fehlermeldung</strong> zu Gesicht, denn die 5,5 ist eben keine Zahl aus dem Bereich der ganzen Zahlen.</p>
<p>Aber wie speichern wir nun <strong>reelle Zahlen</strong> (Zahlen mit Komma &#8211; auch Gleitkommazahlen)? Dazu müssen wir einen anderen Datentyp verwenden, nämlich den Datentyp Double oder Float. Der Unterschied zwischen den beiden Typen ist der Speicherplatz, der reserviert wird &#8211; bei Float weniger als bei Double (Float: 4 Bit / Double: 8 Bit). Somit können unterschiedlich lange <strong>Gleitkommazahlen</strong> gespeichert werden. Als Java-Code sieht das folgendermaßen aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">double</span> reelleZahl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5.5</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-92"></span>Es ist darauf zu achten, dass in Java statt dem Komma ein Punkt bei reellen / <strong>Gleitkomma-Zahlen</strong> verwendet wird. Also statt 5,5 schreiben wir im Code 5.5, sonst bekämen wir wieder eine <strong>Fehlermeldung</strong> serviert.</p>
<p>Der <strong>Datentyp Boolean</strong> ist für Bool&#8217;sche Werte da. Bool&#8217;sche Werte sind <strong>Wahrheitswerte</strong> &#8211; also ob etwas wahr oder falsch, oder auf Englisch true oder false ist.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> wahrheitswertWahr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">boolean</span> wahrheitswertFalsch <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>Für einzelne Buchstaben verwendet man den <strong>Datentyp Char</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">char</span> einZeichen <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Hier ist darauf zu achten, dass einzelne Zeichen in einfache Anführungszeichen gesetzt werden müssen.</p>
<p>Natürlich kann man in Java auch mehr als nur einen Buchstaben in einer Variablen speichern. Dies bewerkstelligen wir wiederum mit einem neuen Datentyp &#8211; dem <strong>Datentyp String</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> eineGanzeReiheAnBuchstaben <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;So können ganze Sätze gespeichert werden&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Hier nochmal die Datentypen in einer Übersicht:</p>
<table border="0" cellspacing="0" cellpadding="10">
<tbody>
<tr>
<th align="left">Datentyp:</th>
<th align="left">Schlüsselwort:</th>
<th align="left">Werte:</th>
</tr>
<tr>
<td>Integer</td>
<td>int</td>
<td>Ganze Zahlen</td>
</tr>
<tr>
<td>Float</td>
<td>float</td>
<td>Gleitkommazahlen (4 Bit)</td>
</tr>
<tr>
<td>Double</td>
<td>double</td>
<td>Gleitkommazahlen (8 Bit)</td>
</tr>
<tr>
<td>Char</td>
<td>char</td>
<td>einzelne Buchstaben</td>
</tr>
<tr>
<td>String</td>
<td>String</td>
<td>Zeichenketten</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-datentypen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: Variablen</title>
		<link>http://coder-blog.de/java-tutorial-variablen/</link>
		<comments>http://coder-blog.de/java-tutorial-variablen/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 22:24:07 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[informatik]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[lernen]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>
		<category><![CDATA[studium]]></category>
		<category><![CDATA[variablen]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=81</guid>
		<description><![CDATA[In diesem Java-Tutorial lernen wir was Variablen sind und was man mit ihnen anstellen kann.]]></description>
			<content:encoded><![CDATA[<p>In diesem <strong>Java-Tutorial</strong> lernen wir was Variablen sind und was man mit ihnen anstellen kann.</p>
<p>Variablen werden viele schon aus der Mathematik kennen. Schauen wir und beispielsweise eine einfache Gleichung aus der Algebra an.<br />
<code>x² + 2x = 0</code><br />
Dabei ist x eine Variable, die für verschiedene Zahlen steht. Dass x die Werte 0 und -2 annehmen kann, damit die Gleichung auch korrekt ist, ist hier unwichtig.</p>
<p>Eine Variable in der Programmierung kann man sich als Box vorstellen, in die wir verschiedenste Werte reinstecken und diese dann wieder auslesen oder verändern können.<br />
Bildlich können wir uns das so vorstellen:</p>
<div id="attachment_82" class="wp-caption alignnone" style="width: 211px"><img class="size-full wp-image-82" title="Java-Tutorial: Variablen" src="http://coder-blog.de/wp-content/uploads/2009/03/tut_var.gif" alt="Variablen Bildlich" width="201" height="60" /><p class="wp-caption-text">Variablen Bildlich</p></div>
<p>Die Variable ist eine Box, die den Namen &#8220;zahl&#8221; trägt. In diese Box können wir nun zum Beispiel eine Zahl &#8220;hineinschieben&#8221; &#8211; hier ist es die 5. Später können wir auf diese gespeicherte 5 zugreifen, indem wir einfach dazu den Namen der Variablen verwenden.</p>
<p><span id="more-81"></span>Dies wollen wir nun in Java mit folgendem Code übertragen.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> zahl<span style="color: #339933;">;</span>
zahl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span></pre></div></div>

<p>In der ersten Zeile sagen wir dem Compiler, dass wir eine Variable zahl in unserem Programm haben werden. In der zweiten Zeile weisen wir der Variablen zahl den Wert 5 zu.<br />
Den obigen Code können wir so auch verkürzt aufschreiben:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> zahl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span></pre></div></div>

<p>Jetzt kann man beim weiteren Programmieren auf diese Variable zugreifen, sie verändern oder mit ihr Rechnungen durchführen. Dazu dieses Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> VariablenTutorial
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">int</span> zahl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ich bin die Variable zahl! Mein Wert ist: &quot;</span> <span style="color: #339933;">+</span> zahl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    zahl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ich bin die Variable zahl! Mein Wert ist: &quot;</span> <span style="color: #339933;">+</span> zahl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    zahl <span style="color: #339933;">=</span> zahl <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ich bin die Variable zahl! Mein Wert ist: &quot;</span> <span style="color: #339933;">+</span> zahl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die Ausgabe dieses Programmes sollte wie folgt aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Ich bin die Variable zahl<span style="color: #000000; font-weight: bold;">!</span> Mein Wert ist: <span style="color: #000000;">5</span>
Ich bin die Variable zahl<span style="color: #000000; font-weight: bold;">!</span> Mein Wert ist: <span style="color: #000000;">3</span>
Ich bin die Variable zahl<span style="color: #000000; font-weight: bold;">!</span> Mein Wert ist: <span style="color: #000000;">6</span></pre></div></div>

<p>Was geschieht nun in unserem Programmablauf?<br />
In der ersten Zeile der main-Methode erstellen wir eine Variable zahl, die den Wert 5 zugewiesen bekommt. Danach rufen wir System.out.println() mit einem Text auf und übergeben unsere Variable. In der dritten Zeile überschreiben wir unsere Variable mit einem neuen Wert und geben wieder eine Meldung auf der Konsole aus. In der fünften Zeile rechnen wir mit dieser Variable. Der Wert der Variable zahl, der jetzt 3 ist, wird mit 3 addiert und das Ergebnis wird wieder in unsere Variable zahl gespeichert. Eine erneute Ausgabe auf der Konsole zeigt uns, dass die Variable zahl jetzt den Wert 6 angenommen hat.<br />
Was dieses &#8220;int&#8221; da vor der Variable zu suchen hat, klären wir im nächsten Tutorial. Freut euch drauf!</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-variablen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: CowSay (naiv)</title>
		<link>http://coder-blog.de/java-tutorial-cowsay-naiv/</link>
		<comments>http://coder-blog.de/java-tutorial-cowsay-naiv/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 21:41:00 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[javakurs]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>
		<category><![CDATA[studium]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=57</guid>
		<description><![CDATA[In desem Java-Tutorial erstellen wir uns das aus Unix bekannte cowsay-Programm in mini-Ausgabe.]]></description>
			<content:encoded><![CDATA[<p>Unsere Kenntnisse aus dem <strong><a title="Java-Tutorial: HelloWorld" href="http://coder-blog.de/java-tutorial-helloworld/" target="_blank">HelloWorld-Tutorial</a></strong> geben uns die Möglichkeit das in Unix-Systemen aufzufindende Programm <strong>cowsay</strong> nachzubilden. Wer ein solches System laufen hat, kann gerne diesen Befehl ausprobieren, um zu sehen wie unser Ergebnis aussehen wird.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">cowsay <span style="color: #ff0000;">&quot;Java rocks!&quot;</span></pre></div></div>

<p>Die Ausgabe sollte in etwa so aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> _____________
<span style="color: #000000; font-weight: bold;">&lt;</span> Java rocks<span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000; font-weight: bold;">&gt;</span>
 <span style="color: #660033;">-------------</span>
       \   ^__^
        \  <span style="color: #7a0874; font-weight: bold;">&#40;</span>oo<span style="color: #7a0874; font-weight: bold;">&#41;</span>\_______
           <span style="color: #7a0874; font-weight: bold;">&#40;</span>__<span style="color: #7a0874; font-weight: bold;">&#41;</span>\       <span style="color: #7a0874; font-weight: bold;">&#41;</span>\<span style="color: #000000; font-weight: bold;">/</span>\
               <span style="color: #000000; font-weight: bold;">||</span>----w <span style="color: #000000; font-weight: bold;">|</span>
               <span style="color: #000000; font-weight: bold;">||</span>     <span style="color: #000000; font-weight: bold;">||</span></pre></div></div>

<p><span id="more-57"></span>Falls ihr aber Windows benutzen solltet, so braucht ihr nicht enttäuscht zu sein, denn wenn ihr diesem Link folgt (<a title="Web-Cowsay" href="http://user.cs.tu-berlin.de/~mutax/cgi-bin/cowsay.cgi" target="_blank">CowSay</a>), findet ihr ein CowSay als Web-Applikation. Wie es scheint, findet sich cowsay auf Mac OSX nicht automatisch vorinstalliert. Will man dieses Programm unbedingt auf dem Rechner haben, so kann man es sich über die <a title="DarwinPorts" href="http://darwinports.com" target="_blank">DarwinPorts</a> oder <a title="MacPorts" href="http://www.macports.org" target="_blank">MacPorts</a> nachinstallieren. Danke an Martin für diesen Hinweis.</p>
<p>Nun wollen wir mit unserem <strong>Java CowSay</strong> beginnen. Wir werden für jede Zeile eine <strong>System.out.println() </strong>-Anweisung mit dem entsprechenden Inhalt schreiben. So würde beispielsweise der Aufruf für die erste Zeile lauten</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; _____________&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Das wollen wir nun für jede Zeile fortführen. Diesen Code packen wir in der Klasse CowSay in die Methode main(). Dann sollte unser Quelltext wie folgt aussehen</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CowSay
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; _____________ &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt; Java rocks! &gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;       <span style="color: #000099; font-weight: bold;">\\</span>   ^__^&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;        <span style="color: #000099; font-weight: bold;">\\</span>  (oo)<span style="color: #000099; font-weight: bold;">\\</span>_______&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;           (__)<span style="color: #000099; font-weight: bold;">\\</span>       )<span style="color: #000099; font-weight: bold;">\\</span>/<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;               ||----w |&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;               ||     ||&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Euch wird sicher auffallen, dass wir statt einem Backslash ( \ ) zwei Backslashes ( \\ ) hintereinander geschrieben haben. Dies liegt daran, dass das Backslash in Java &#8211; und einigen anderen Programmiersprachen auch &#8211; ein <strong>Escape-Zeichen</strong> ist, womit bestimmte <strong><a title="Wikipedia: Escape-Sequenzen" href="http://de.wikipedia.org/wiki/Escape-Sequenz" target="_blank">Escape-Sequenzen</a></strong> eingeleitet werden. Was Escape-Sequenzen sind behandeln wir in einem weiteren Tutorial.</p>
<p>Unseren Quellcode speichern wir analog zu unserem <strong>HalloWelt-Tutorium</strong> unter CowSay.java, compilieren diese Datei mit dem <strong>Java Compiler</strong> und führen den entstandenen Bytecode aus.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">javac CowSay.java
java CowSay</pre></div></div>

<p>Und siehe da, wir haben nun eine Kuh auf unserer Konsole. Okay, zugegeben hat unsere Kuh noch einen recht kleinen Wortschatz, denn sie kann immer nur &#8220;Java rocks!&#8221; ausgeben (deshalb auch &#8220;CowSay (naiv)&#8221;). Dies wird sich aber im Laufe der nächsten Tutorials ändern. Also seid gespannt auf die kommenden <strong>Java-Tutorials</strong>!</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-cowsay-naiv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java-Tutorial: HelloWorld</title>
		<link>http://coder-blog.de/java-tutorial-helloworld/</link>
		<comments>http://coder-blog.de/java-tutorial-helloworld/#comments</comments>
		<pubDate>Tue, 24 Mar 2009 23:02:03 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[coder-blog.de]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[java tutorials]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[javakurs]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>
		<category><![CDATA[oop programmierung]]></category>
		<category><![CDATA[studium]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=30</guid>
		<description><![CDATA[In diesem Java-Tutorial erstellen wir ein einfaches HelloWorld - Programm.]]></description>
			<content:encoded><![CDATA[<p><strong>In diesem Java-Tutorial erstellen wir ein einfaches HelloWorld &#8211; Programm.</strong></p>
<p>Zunächst einmal kann man jedes <strong>Java-Programm</strong> in einem stinknormalen Editor schreiben. Daneben gibt es auch Editoren, die bestimmten Code besonders hervorheben &#8211; das <strong>Syntax-Highlighting</strong>. Einige bekannte sind hier <a title="Projektseite Gedit" href="http://projects.gnome.org/gedit/" target="_blank"><strong>gedit</strong></a> für Gnome und <a title="Projektseite Kate" href="http://www.kate-editor.org/" target="_blank"><strong>kate</strong></a> für KDE. Beide für Linux-Betriebssysteme. Für Windows bietet sich beispielsweise das <a title="Projektseite Notepad++" href="http://notepad-plus.sourceforge.net/de/site.htm" target="_blank"><strong>Notepad++</strong></a> an.</p>
<p>Daneben gibt es auch sogenannte <strong>integrierte Entwicklungsumgebungen</strong> &#8211; die IDEs. Diese bieten in der Regel neben Syntax-Highlighting, einen Compiler bzw. Interpreter, einen Debugger, einen Linker und viele andere tolle Features. Bekannte Vertreter sind hierbei für Java <a title="Internetpräsenz von NetBeans" href="http://www.netbeans.org/" target="_blank">NetBeans</a> und <a title="Internetpräsenz von eclipse" href="http://www.eclipse.org/" target="_blank">eclipse</a>. IDEs sind für Einsteiger jedoch nicht zu empfehlen, da sie unter anderem einem viel Arbeit vorweg nehmen, dies jedoch am Anfang den Lernerfolg eher behindert.</p>
<p>Kommen wir nun zu unserem ersten Programm &#8211; dem HelloWorld</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HelloWorld&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><span id="more-30"></span>Diesen Code speichern wir nun unter dem Dateinamen HelloWorld.java ab. Bei <strong>Java-Programmen</strong> ist darauf zu achten, dass Dateinamen mit dem Namen der Klasse identisch sind, da sonst ein Fehler erzeugt wird. Die genauen Bedeutungen der Aufrufe werden noch im Laufe der Zeit in den Tutorien geklärt. Aber eine kurze Beschreibung dürfte dennoch nicht schaden.</p>
<p>Wir erzeugen in der ersten Zeile eine Klasse mit dem Namen HelloWorld. In dieser Klasse definieren wir die Methode main. Die main-Methode in Java ist ein besonderes Konstrukt und wird deshalb auch speziell behandelt. In der fünften Zeile erzeugen wir die Bildschirmausgabe HelloWorld mit dem Befehl <strong>System.out.println(&#8221;HelloWorld&#8221;);</strong>. Dabei wird am Ende automatisch in eine neue Zeile gesprungen.</p>
<p>Jetzt wollen wir unser kleines HelloWorld-Programm noch compilieren und dann ausführen.</p>
<p>Den ersten Schritt erledigen wir mit dem im <a title="Downloadsite des JDK" href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">Java Development Kit</a> (JDK) mitgelieferten <strong>Java Compiler</strong>. Dazu lautet der Befehl, den wir in der Konsole / Eingabeaufforderung eingeben müssen wie folgt. Beim Compilieren erstellt uns der Java Compiler aus unserer HelloWorld.java den sogenannten Bytecode. Dabei wird die Datei HelloWorld.class erzeugt, die man auf allen Betriebssystemen lauffähig sein sollte. Vorausgesetzt, auf dem anderen Betriebssystem ist mindestens das <a title="Downlaodseite JRE" href="http://java.com/de/download/manual.jsp" target="_blank">Java Runtime Environment</a> (JRE) installiert.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">javac HelloWorld.java</pre></div></div>

<p>So, haben wir nun keine Fehlermeldung ausgegeben bekommen, ist alles gut gelaufen und wir können nun mit der Ausführung unseres Programms jetzt fortfahren. Dies bewerkstelligen wir mit</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">java HelloWorld</pre></div></div>

<p>Man sollte darauf achten, dass hier keine Dateiendung angegeben werden darf. Als Ausgabe solltet ihr nur ein kurzes &#8220;HelloWorld&#8221; auf der Konsole / Eingabeaufforderung sehen.</p>
<p>Ich hoffe, ihr hattet viel Spaß mit dem ersten kleinen <strong>Java-Tutorial</strong> und freut euch auf die kommenden! <img src='http://coder-blog.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-helloworld/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
