<?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; java-tutorial</title>
	<atom:link href="http://coder-blog.de/tag/java-tutorial/feed" rel="self" type="application/rss+xml" />
	<link>http://coder-blog.de</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Fri, 30 Mar 2012 08:43:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java-Tutorial: Methoden</title>
		<link>http://coder-blog.de/java-tutorial-methoden</link>
		<comments>http://coder-blog.de/java-tutorial-methoden#comments</comments>
		<pubDate>Fri, 08 Apr 2011 00:08:48 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[informatik]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[methoden]]></category>
		<category><![CDATA[programmfluss]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=328</guid>
		<description><![CDATA[Methoden dienen dazu, um Coder durch Auslagern übersichtlicher und wartungsfreundlicher zu gestalten. Beim Aufruf einer Methode wird die aufrufende Methode unterbrochen, bis die aufgerufene Methode abgearbeitet wurde. Eine Methode stellt somit ein kleines Unterprogramm dar. <a href="http://coder-blog.de/java-tutorial-methoden">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wie der Titel schon erahnen lässt, beschäftigt sich dieser Beitrag mit Java Methoden. Wer Funktionen aus anderen Programmiersprachen wie beispielsweise PHP, C usw. schon kennt, wird sich mit Methoden schnell anfreunden können. Um zu verdeutlichen wie man mit Methoden arbeitet, sollten wir uns zunächst folgenden Code anschauen.</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;">/**
 * Klasse Programm soll die Verwendung von Methoden verdeutlichen.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Programm <span style="color: #009900;">&#123;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Methode add addiert zwei uebergebene Parameter und gibt die Summe zurueck
     * @param summand_1 Der erste Summand
     * @param summand_2 Der zweite Summand
     * @return Das Ergebnis der Addition beider  Summanden
     */</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;">int</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> summand_1, <span style="color: #000066; font-weight: bold;">int</span> summand_2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> summand_1 <span style="color: #339933;">+</span> summand_2<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Methode hallo gibt ihren Namen aus. Welche weitere Methode sie aufgerufen hat und
     * welches Ergebnis sie von der anderen Methode erhalten hat.
     */</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> hallo<span style="color: #009900;">&#40;</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;Ich bin die Methode <span style="color: #000099; font-weight: bold;">\&quot;</span>hallo<span style="color: #000099; font-weight: bold;">\&quot;</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;Ich rufe die Methode <span style="color: #000099; font-weight: bold;">\&quot;</span>add<span style="color: #000099; font-weight: bold;">\&quot;</span> mit den Parametern 3 und 4 auf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> summe <span style="color: #339933;">=</span> add<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</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;Ich habe als Ergebnis die Summe &quot;</span> <span style="color: #339933;">+</span> summe <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; von <span style="color: #000099; font-weight: bold;">\&quot;</span>add<span style="color: #000099; font-weight: bold;">\&quot;</span> erhalten&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Main-Methode wird vom Compiler gesucht und automatisch aufgerufen
     */</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;Das ist ein Beispiel, das die Verwendung von Methoden erklaeren soll&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        hallo<span style="color: #009900;">&#40;</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;Hier endet die main-Methode und damit das Programm&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-328"></span><br />
Dieses Programm enthält insgesamt drei Methoden, die wir definiert haben. Zunächst ist da die bereits bekannte main-Methode. Diese wird beim Kompilieren und beim Ausführen des Programms gesucht und automatisch aufgerufen. Sie ermöglicht also der JVM den Einstieg in unser Programm. Ohne ihr würde nichts funktionieren. Wichtig ist, dass ein Programm nur eine main-Methode haben darf, da sonst nicht klar ist, welche dieser Methoden ausgeführt werden sollen.<br />
Die zweite Methode ist die Methode &#8220;hallo&#8221;. Sie ähnelt in ihrem Methodenkopf sehr stark der main-Methode. Der einzige Unterschied ist, dass unsere hallo-Methode keine Parameter übergeben bekommt. Parameter sind die Elemente, die von der runden öffnenden und schließenden Klammer, die hinter der Bezeichnung der Methode kommt, eingeschlossen werden. Zu erkennen ist, dass die main-Methode einen Parameter mit der Bezeichnung &#8220;args&#8221; übergeben bekommt. Dieser Parameter ist vom Typ ein String-Array.</p>
<p>Die Methode &#8220;hallo&#8221; ruft in Zeile 22 eine weitere Methode auf, die wir &#8220;add&#8221; genannt haben. Das Ergebnis, dass uns die Methode &#8220;add&#8221; liefert, wird der Variablen &#8220;summe&#8221; zugewiesen. Der Wert der Variablen wird dann in der drauf folgenden Zeile ausgegeben.<br />
Die Methode &#8220;add&#8221; addiert die beiden übergebenen Parameter. Das Ergebnis der Addition wird dann zurückgegeben. Das Zurückgeben erfolgt stets mit dem Schlüsselwort &#8220;return&#8221;. Sobald wir etwas zurückgeben möchten, müssen wir im Methodenkopf notieren welchen Typ die Rückgabe haben wird. Deshalb steht vor der Bezeichnung der add-Methode das Schlüsselwort &#8220;int&#8221;. Zurückgegeben werden können alle erdenklichen Typen wie int, long, float, double, String, Arrays und Objekte. Da wird in der Methode &#8220;hallo&#8221; nichts zurückgeben wollen, sondern nur Ausgaben tätigen, muss vor dem Methodennamen das Schlüsselwort &#8220;void&#8221; stehen. Es heißt soviel wie &#8220;Ich geben nichts zurück&#8221;. Die main-Methode darf per Konvention nur void sein.</p>
<p>Was ist nun der Sinn hinter Methoden? Methoden dienen dazu, um Code durch Auslagern übersichtlicher und wartungsfreundlicher zu gestalten.</p>
<p>Methoden werden nacheinander wie sie im Programm aufgerufen werden abgearbeitet. Methoden, die andere Methoden aufrufen, werden unterbrochen und fahren erst nachdem die aufgerufene Methode beendet ist, in ihrem Programmfluss fort. In unserem Beispiel wird zunächst du die JVM die main-Methode ausgeführt. In dieser Methode rufen wir die Methode &#8220;hallo&#8221; auf. Somit wird die Methode main zunächst unterbrochen und die hallo-Methode ausgeführt. Methode &#8220;hallo&#8221; ruft wiederum die Methode &#8220;add&#8221; auf, der sie die zwei Parameter 3 und 4 übergibt, um dann den eigenen Programmfluss zu unterbrechen und den Weg für &#8220;add&#8221; frei zu machen. Nachdem &#8220;add&#8221; die beiden Parameter addiert hat, kann &#8220;hallo&#8221; fortfahren und ebenfalls beendet werden. Jetzt wird weiter die Methode &#8220;main&#8221; ausgeführt, bis diese abgearbeitet wurde und unser Programm schließlich beendet werden kann.<br />
Den Programmfluss soll das nachfolgende Bild darstellen.</p>
<div id="attachment_338" class="wp-caption aligncenter" style="width: 307px"><a href="http://coder-blog.de/wp-content/uploads/methoden.gif"><img class="size-full wp-image-338 " title="Programmfluss der Methoden" src="http://coder-blog.de/wp-content/uploads/methoden.gif" alt="Programmfluss der Methoden main, hallo und add" width="297" height="201" /></a><p class="wp-caption-text">Programmfluss der Methoden</p></div>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-methoden/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<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 20: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 <a href="http://coder-blog.de/java-tutorial-cowsay-kuh-lernt-sprechen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Lange versprochen wenden wir uns jetzt unserer Kuh aus dem zweiten Java-Tutorial zu. Schauen wir uns noch einmal an wie das Unix-Programm cowsay 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 Java Runtime Environment unterstützt werden.<span id="more-78"></span><br />
Okay, fangen wir dann mal an. Zunächst erstellen wir uns eine Klasse cowsay, die auch die main-Methode 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 Java-Code kommt. Dazu ist dieses String[] args da! Wie wir in den letzten Tutorials gelernt haben, handelt es sich hierbei um ein Array 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;ArrayIndexOutOfBoundException&#8221; erhalten, wenn wir beim Programmaufruf keine Nachricht übergeben. Dies werden wir mit einer bedingten Anweisung 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;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> contentAndBorders <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> bottom <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&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 Schleife 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 CowSay-Tutorial 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;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> contentAndBorders <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    speechBubble <span style="color: #339933;">+=</span> bottom <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&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;          ^__^&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&quot;</span><span style="color: #339933;">;</span>
    kuh <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot;          (oo)_______&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;n&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> <span style="color: #0000ff;">&quot;n&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;n&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.</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 14: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 <a href="http://coder-blog.de/java-tutorial-arrays">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wenden wir uns in diesem Tutorial einmal den Arrays 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. Bei späteren Berechnungen sollte man daran denken, dass ein Array mit dem Index 0 beginnt.</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 Array-Objekt, 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 Compilerfehler 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 Array.length 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 for-Schleife 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 ArrayIndexOutOfBoundsException Fehlermeldung.</p>
<p>Wie in anderen Programmiersprachen ist es uns in Java auch möglich mehrdimensionale Arrays 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 zweidimensionales Array und befüllen es mit den Werten 1 und 2. Danach durchlaufen wir dieses Array mit zwei ineinander geschachtelten for-Schleifen, 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 erweiterte for-Schleife, 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 Arrays in Java 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 19: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. <a href="http://coder-blog.de/java-tutorial-schleifen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In diesem Tutorial werden wir uns den Schleifen 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 Schleifenbedingung und dem Rumpf. 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 while-Schleife 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 while-Schleife 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 Endlosschleife, 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 for-Schleife 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 for-Schleife 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 for-Schleife 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 do-while-Schleife 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 do-while-Schleife unterscheidet sich von der while-Schleife einmal, dass da das &#8220;do&#8221; vor dem &#8220;while&#8221; steht und dadurch, dass eine do-while-Schleife mindestens einmal ausgeführt wird. Haben wir also eine Bedingung, die von vornherein &#8220;false&#8221; ist, so findet bei der while- und der for-Schleife keine Ausführung des Codes im Rumpf statt &#8211; jedoch bei der do-while-Schleife.</p>
<p>Nun sollte sich die Frage stellen wann man welche Schleife verwenden sollte. Bei der do-while-Schleife 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 22: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. <a href="http://coder-blog.de/java-tutorial-kommentare">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Als Kommentare bezeichnet man in Programmiersprachen besondere Code-Teile, die vom Compiler nicht in Maschinencode – in Java Bytecode – übersetzt werden, sondern nur den Lesern beziehungsweise den Programmierern dienen.<br />
Neben selbst sprechenden Namen 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 Zeilenkommentar gilt nur für eine Zeile und wird mit einem // eingeleitet. Möchte man Kommentare über mehrere Zeilen hinweg setzten, so bietet sich das Blockkommentar dafür an. Dieses wird mit einem /* eingeleitet und endet mit */.<br />
Das JavaDoc-Kommentar 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 javadoc ist es möglich diese Kommentare zu einer API-Dokumentation 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 JavaDoc-Kommentar, der besondere Schlüsselwörter wie @author enthält, die vom javadoc-Tool ausgewertet werden können.<br />
Der zweite Kommentar auf den wir stoßen ist ein Blockkommentar, der sich über drei Zeilen hinzieht.<br />
Bei dem dritten und vierten Kommentar verwenden wir nun den Zeilenkommentar, 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 09: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 <a href="http://coder-blog.de/java-tutorial-operatoren">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In den letzten Java-Tutorials haben wir fast instinktiv Operatoren genutzt und ich habe noch einige logische Operatoren 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 Zuweisungsoperator =, 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 * &#8211; Operator multipliziert zwei Werte miteinander. Die Variable ersteVariable und die Zahl 2 werden hierbei als Operanden bezeichnet.<span id="more-114"></span><br />
In Java findet man im Grunde drei Arten von Operatoren. So gibt es die unären Operatoren, die nur auf einen Operanden Anwendung finden. Der große Bruder davon ist der binäre Operator, oder auch zweistellige Operator. Diese sind beispielsweise Operatoren, die in der Mathematik gebraucht werden – also Plus, Minus, Mal und Geteilt. Wie man auch vermuten kann, kommt jetzt der dreistellige Operator. So gibt es auch den Fragezeichenoperator (?-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 arithmetische Operatoren (+, -, *, /, %). Der %-Operator, oder auch Modulo-Operator / Restwert-Operator 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 Verbundoperatoren. 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 überladene Plus (+) für Strings. 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 Präfix-Inkrement bzw. Postfix-Inkrement und Präfix-Dekrement bzw. Postfix-Dekrement 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 Kleiner (&lt;), Größer (&gt;), Kleiner-gleich (&lt;=), und Größer-gleich (&gt;=). Diese können wir in Java als Vergleichsoperatoren 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 Vergleichsoperatoren 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 boolschen Wert, 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 logischen Operatoren 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 AND-Operator liefert uns nur ein Wahr, wenn beide Ausdrücke, die miteinander verbunden werden auch wahr sind. Dagegen liefert uns der OR-Operator 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 ||-Operator 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 XOR-Operatoren. 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 Java-Tutorial behandeln werden ist der Nicht-Operator. 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 Nicht-Operator 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 22: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 <a href="http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Wie in anderen Programmiersprachen, so bietet natürlich auch Java die Möglichkeit der Fallunterscheidung zu nutzen, die auch in mancher Literatur als bedingte Anweisung beschrieben wird.<br />
Ohne Fallunterscheidungen wäre ein Programm ziemlich doof, denn es könnte nicht flexibel Programmteile ausführen, die nur unter einer bestimmten Bedingung aufgerufen werden soll. Dazu bietet Java die if- bzw. if/else-Anweisungen und die switch-Anweisung.</p>
<p>Bedingte Anweisungen sind gar nicht so schwer – also fangen wir gleich mit der if-Anweisung 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 if-Anweisung, die – wie jede if-Anweisung – mit dem Schlüsselwort if eingeleitet wird und in Klammern einen boolschen Wert 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 Operator != vergleicht zwei Werte auf Nicht-Gleichheit und liefert uns einen boolschen Wert als Ergebnis. Als Gegenstück dazu gibt es den == Operator, der zwei Werte auf Gleichheit 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 if-Anweisung 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 < Operator 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 if-/else-Anweisung. Fügen wir nun unserem if- einen else-Zweig 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 verschachtelte Fallunterscheidungen 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 if-Anweisung nicht zutrifft, wird in die else-Anweisung gesprungen. Darin befindet sich nun ebenfalls eine if-/else-Anweisung. 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 else-if-Anweisung ist eine Kombination der if – und else-Anweisung. Denn trifft nun der if-Fall bei uns nicht zu, dann wird der else-if-Fall überprüft. Trifft dieser auch nicht zu, wird else 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 if-Anweisung und der if-/else-Anweisung findet man die switch-Anweisung:</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 switch-Anweisung 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 default-Fall angewandt. Der default-Fall ist optional und kann auch weggelassen werden. Bei switch ist zu beachten, dass die break-Anweisungen 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 Fall-Through. 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 Arrays erfahren und wir können unser CowSay-Programm 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 21: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. <a href="http://coder-blog.de/java-tutorial-datentypen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In diesem Java-Tutorial erfahren wir was Datentypen sind und lernen einige davon kennen.</p>
<p>&#8220;Aber was soll eigentlich dieses &#8216;int&#8217; da?&#8221; hat man sich im <a title="Java-Tutorial: Variablen" href="http://coder-blog.de/java-tutorial-variablen/" target="_self">Java-Tutorial: Variablen</a> 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 Datentyp Integer 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 Fehlermeldung zu Gesicht, denn die 5,5 ist eben keine Zahl aus dem Bereich der ganzen Zahlen.</p>
<p>Aber wie speichern wir nun reelle Zahlen (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 Gleitkommazahlen 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 / Gleitkomma-Zahlen verwendet wird. Also statt 5,5 schreiben wir im Code 5.5, sonst bekämen wir wieder eine Fehlermeldung serviert.</p>
<p>Der Datentyp Boolean ist für Bool&#8217;sche Werte da. Bool&#8217;sche Werte sind Wahrheitswerte &#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 Datentyp Char.</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 Datentyp String.</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 21: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. <a href="http://coder-blog.de/java-tutorial-variablen">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In diesem Java-Tutorial 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.</p>
<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 20: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. <a href="http://coder-blog.de/java-tutorial-cowsay-naiv">mehr <span class="meta-nav">&#187;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Unsere Kenntnisse aus dem <a title="Java-Tutorial: HelloWorld" href="http://coder-blog.de/java-tutorial-helloworld/" target="_blank">HelloWorld-Tutorial</a> geben uns die Möglichkeit das in Unix-Systemen aufzufindende Programm cowsay 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 Java CowSay beginnen. Wir werden für jede Zeile eine System.out.println()-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;          ^__^&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;          (oo)_______&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: #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 Escape-Zeichen ist, womit bestimmte <a title="Wikipedia: Escape-Sequenzen" href="http://de.wikipedia.org/wiki/Escape-Sequenz" target="_blank">Escape-Sequenzen</a> eingeleitet werden. Was Escape-Sequenzen sind behandeln wir in einem weiteren Tutorial.</p>
<p>Unseren Quellcode speichern wir analog zu unserem HalloWelt-Tutorium unter CowSay.java, compilieren diese Datei mit dem Java Compiler 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 Java-Tutorials!</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-cowsay-naiv/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 3.058 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-19 18:20:19 -->

