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.
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.
Grundlegende Syntax weitgehend identisch mit zu C/C++
- Kommentare
- Semikolon ';' am Zeilenende
- primitive Datentypen
- Operationen, Kontrollstrukturen
Erstellen des Quellprogramms:
JAVA Konvention:
- Filename des Quellprogramms hat Endung
.java
-
Basisname des Files ist der
Klassenname (
HelloWorld)
Editieren, z.B.:
gedit HelloWorld.java
(Tip: copy&paste mit Maus aus Web-Browser)
Kompilieren:
javac HelloWorld.java
erzeugt byte-code
HelloWorld.class
Ausführen:
java HelloWorld
Byte-code (.class-files) überall lauffähig (Linux, Unix, Windows, ...)
passender JAVA Interpreter nötig (JAVA VM), der systemabhängige Übersetzung des
byte-codes ausführt.
GDuckeck
2013-02-28