| Java™ und Objektorientierung | |
| Java/OO | Druckversion |
Die folgende Tabelle beinhaltet eine Auflistung der gängigen Anweisungen in Java (JSKD 1.5). Anweisungen stehen innherhalb von Methoden (oder genereller: innerhalb von Blöcken) und werden sequenziell vom Interpreter abgearbeitet.
| Syntax | Beispiel | |||
| Deklaration | ||||
| Type variable; | int x; GUI mainDialog; |
|||
| Type variable = Ausdruck; | boolean b = true; Printer p = new Printer(); PrintWriter prWr = System.out; |
|||
| Ausdruck | |
|||
| Zuweisung | x = 4; main = new Main(); |
|||
| Inkrement, Dekrement | x++; |
|||
| Methodenaufruf |
println(x);
file.write();
System.out.println("Hallo Welt!");
MyApplet.start(); |
|||
| Instanzierung | new GuiApplication(); |
|||
| Verzweigungen, Schleifen | ||||
while(BED.)
|
while(a > 0) {
this.process(a); a--;
}
|
|||
do
while(BED.); |
do {
System.out.print(".");
} while (! endOfFile());
|
|||
if(BED.)
[else if(BED.)
[else
|
if(t < 0)
print("glatteisgefahr");
|
|||
|
switch(AUSDRUCK) { case KONST_AUSDRUCK:
case KONST_AUSDRUCK:
. . [default :
|
switch(note)
{
case 1:
print("sehr ");
case 2:
print("schwach");
break;
case 3:
print("genügend");
break;
.
.
.
case 6: case 7:
print("sehr gut");
break;
default:
print("unbekannte Note!");
}
|
|||
for(Variable : Collection) {
|
for(Person p : allePersonen) {
String name = p.getName();
System.out.println("Name :" + name);
}
|
|||
for(INIT_ANW.;
BED.;
NEXT_ANW.)
|
for(i = 0; i < 10; i++)
print("a_i =" + a[i]);
|
|||
|
... obige for-Schleife als "while()": INIT_ANW.; while(BED.) {
|
i = 0;
while(i < 10) {
print("a_i =" + a[i]);
i++;
} |
|||
| die leere Anweisung | ||||
| ; | ;; |
|||
| Die return-Anweisung | ||||
| return [value] | return myPoint.x; |
|||
| Die assert-Anweisung | ||||
| assert ( |
assert (month <= 12) : "month > 12!"; |
|||
| Die throw-Anweisung | ||||
| throw |
throw new MyException("My Error Message"); |
|||
| Lokale Klassendeklaration | ||||
|
[modifiers] class <class-name> [extends ...] [implements ...] { [element]* } |
class MyHandler implements ActionListener
{
public void actionPerformed(ActionEvent ev)
{
System.out.println("Action: " + ev);
}
}
|
|||
| Anonyme Objekterzeugung | ||||
|
new <class-name> (<args>) { <body> } |
new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
};
|
|||
| Sprungmarken (Labels) | ||||
| <label-name> : Schleifenkonstrukt |
outer:
for(int i = 0; i < maxi; i++) {
for(int j = 0; j < maxj; j++) {
if(5 == j)
continue outer;
handle(i, j);
}
}
|
|||
| Synchronisationsanweisung | ||||
| synchronized (object) [BLOCK] | synchronized (firstStack) {
firstStack.pop();
secondstack.push();
}
|
|||
| Exception Handling | ||||
| try [BLOCK] catch (Exception-Parameter) [BLOCK] |
try {
str = inputStream.readLine();
} catch (IOException ioe) {
System.out.println("Error : " + ioe);
}
|