JAVA - Hello World

Das Standard-Minimal-Programm:



1: public class HelloWorld {
2:     // A program to display the message
3:     // "Hello World!" on standard output
4:     
5:     public static void main(String[] args) {
6:     System.out.println("Hello World!");
7:     }
8: }   // end of class HelloWorld
9: 


1
Definition einer Klasse mit Namen HelloWorld.
2/3
Kommentare die Funktionalität beschreiben, alles nach // wird ignoriert
5-7
Definition der Funktion main, das ist eine besondere Funktion, die vom JAVA Interpreter als erstes aufgerufen wird.
6
Function call System.out.println(...).
Funktion aus der JAVA System library wird gerufen.
\ensuremath{\color{dgreen}{\Rightarrow}} Ausgabe des übergebenen Argumentes ``Hello World!'' auf Standard-ausgabe, anschließend Zeilenvorschub.
6..;
Semikolon ; wichtig, schliesst jedes Statement ab.
1,3..7,8
Geschweifte Klammern {...} schliessen Block von Code und Deklarationen ein.

\includegraphics[width=0.4\textwidth]{klasse}


Grundlegende Syntax weitgehend identisch mit zu C/C++

Erstellen des Quellprogramms:
JAVA Konvention:

Editieren, z.B.:
gedit HelloWorld.java
(Tip: copy&paste mit Maus aus Web-Browser)


Image javacomp

Kompilieren:
javac HelloWorld.java

\ensuremath{\color{dgreen}{\Rightarrow}} erzeugt byte-code HelloWorld.class

Ausführen:
java HelloWorld

Byte-code (.class-files) überall lauffähig (Linux, Unix, Windows, ...)
\ensuremath{\color{dgreen}{\Rightarrow}} passender JAVA Interpreter nötig (JAVA VM), der systemabhängige Übersetzung des byte-codes ausführt.

GDuckeck 2013-02-28