Arithmetik
| Operator | Bedeutung | Beispiel | Ergebnis |
|---|---|---|---|
+ - * | Grundrechenarten | 6 * 7 | 42 |
/ | Division (bei int: ganzzahlig!) | 7 / 2 | 3 |
% | Rest (Modulo) | 7 % 2 | 1 |
++ -- | um 1 erhöhen/verringern | i++ | |
+= -= *= /= %= | kombinierte Zuweisung | x += 5 |
public class Rechnen {
public static void main(String[] args) {
int a = 17, b = 5;
System.out.println(a + " + " + b + " = " + (a + b));
System.out.println(a / b + " Rest " + a % b);
System.out.println((double) a / b);
int i = 5;
System.out.println(i++ + " " + i); // erst benutzen, dann erhöhen
System.out.println(++i + " " + i); // erst erhöhen, dann benutzen
i += 10; i *= 2;
System.out.println(i);
System.out.println(Math.pow(2, 10) + " " + Math.sqrt(144) + " " + Math.abs(-7) + " " + Math.max(3, 9));
System.out.println(Math.round(2.5) + " " + Math.round(-2.5) + " " + Math.floor(-2.5) + " " + Math.ceil(2.1));
}
}17 + 5 = 22 3 Rest 2 3.4 5 6 7 7 34 1024.0 12.0 7 9 3 -2 -3.0 3.0
Vergleiche und Logik
| Operator | Bedeutung |
|---|---|
== != | gleich / ungleich (bei primitiven Typen) |
< > <= >= | größer, kleiner |
&& | und (mit Kurzschluss) |
|| | oder (mit Kurzschluss) |
! | nicht |
? : | Bedingungsoperator |
public class Logik {
public static void main(String[] args) {
int alter = 20;
boolean hatTicket = true;
System.out.println(alter >= 18 && hatTicket);
System.out.println(alter < 18 || hatTicket);
System.out.println(!hatTicket);
String status = alter >= 18 ? "volljährig" : "minderjährig";
System.out.println(status);
String s = null;
System.out.println(s != null && s.length() > 3); // Kurzschluss: kein Fehler
}
}true true false vollj?hrig false
Bei && wird der rechte Teil nur ausgewertet, wenn der linke true ist. So schützt du dich vor null-Zugriffen.
Bitoperatoren
public class Bits {
public static void main(String[] args) {
int a = 0b1100, b = 0b1010;
System.out.println((a & b) + " " + (a | b) + " " + (a ^ b) + " " + (~a));
System.out.println((1 << 4) + " " + (256 >> 2) + " " + (-8 >>> 28));
System.out.println(Integer.toBinaryString(42) + " " + Integer.toHexString(255) + " " + Integer.bitCount(255));
}
}8 14 6 -13 16 64 15 101010 ff 8
Rangfolge
Von stark nach schwach: Klammern, ++ -- !, * / %, + -, Vergleiche, == !=, &&, ||, ? :, Zuweisungen. Im Zweifel Klammern setzen.
instanceof und Pattern Matching
public class Muster {
public static void main(String[] args) {
Object o = "Hallo";
if (o instanceof String s && s.length() > 3) { // Typ prüfen und gleich benutzen
System.out.println(s.toUpperCase());
}
Object[] dinge = { 42, "Text", 3.5, true };
for (Object d : dinge) {
String art = switch (d) {
case Integer i when i > 10 -> "große Ganzzahl " + i;
case Integer i -> "Ganzzahl " + i;
case String t -> "Text der Länge " + t.length();
default -> "etwas anderes: " + d;
};
System.out.println(art);
}
}
}HALLO gro?e Ganzzahl 42 Text der L?nge 4 etwas anderes: 3.5 etwas anderes: true
Überlauf und Genauigkeit
public class Ueberlauf {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println(max + 1); // springt auf Minimum
try {
Math.addExact(max, 1); // wirft stattdessen eine Ausnahme
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
System.out.println(5 / 2.0 + " " + 1.0 / 0 + " " + 0.0 / 0);
try { System.out.println(5 / 0); } catch (ArithmeticException e) { System.out.println(e.getMessage()); }
}
}-2147483648 integer overflow 2.5 Infinity NaN / by zero
Merke
/beiintschneidet ab,%liefert den Rest==für primitive Typen; Objekte mit.equals()&&und||werten kurz ausbedingung ? a : bist die Kurzform von if/elseinstanceofmit Pattern Matching prüft und benennt in einem SchrittMath.addExactmeldet Überläufe
Aufgabe
Berechne aus Sekunden (z. B. 3725) Stunden, Minuten und Sekunden mit / und %.