JAVA BASICS 2. Primitive Datentypen 1. Warum Java? weit verbreitet einfach und (relativ) sicher keine Pointer (?) keine gotos kein Präprozessor keine globalen Variablen garbage collection objekt-orientiert interpretiert, d.h. plattformunabhängig Programme mit Parallelverarbeitung möglich a) Boolean (logische Werte wahr & falsch) boolean b; boolean b = true; final boolean b = false; (-> Konstantendeklaration) true, false = (Zuweisung),! (Negation), && (und), (oder), == (gleich),!= (ungleich) true && false true false! true boolean b = false;!b Internet - Sprache mh@informatik.tu-cottbus.de 1 / 12 mh@informatik.tu-cottbus.de 2 / 12
b) Character (Zeichen) char a; char a = 'a'; final char b = 'a'; ASCII: 'a', 'b',..., 'z', 'A', 'B',..., 'Z', '0', '1',..., '9' escape char s: '\t' (tab), '\n' (return), '\\' (\), '\ ' ( ) =, ==,!= 'a' == 'b' 'a'!= 'b' char a = 'a'; char b; b = a; c) Integrale Typen byte -128.. 127 8 Bits short -32768.. 32767 16 Bits int -2147483648.. 2147483647 32 Bits long... 64 Bits byte b; short k = -128; final int i = 043; long l = 0xFF; =, ==,!=, +, -, *, /, % (modulo division), ++ (pre/post increment), -- (pre/post decrement) this = 8; that = 2; this = that++ (-> this == 2, that == 3) this = ++that (-> this == 3, that == 3) x = this / that (-> x == 4) x = this % that (-> x == 0) Zuweisungskompatibilität byte < short < int < long, d.h byte kann short- oder int-variablen zugewiesen werden, jedoch nicht umgekehrt. mh@informatik.tu-cottbus.de 3 / 12 mh@informatik.tu-cottbus.de 4 / 12
d) Gleitkomma-Typen float double float f; double d = 0.5; final double pi = 3.14156; 1.3.3 1e1 1e-10 =, ==,!=, +, -, *, /, float f = 1.0; double d = 1e10; double e = 1e100; d = f; (-> d == 1.0) f = d; (Fehler!) f = (float)d; (-> f == 1e10) f = (float)e; (->??) Zuweisungskompatibilität... long < float < double 32 Bits 64 Bits 3. Arrays (Reihungen, Felder) -> Reihungen beliebiger Datentypen byte b[] = new byte[1024]; char[] c, d = new char[5]; int [] p = {1, 2, 3, 4; Index 0.. size - 1 [] length() b[0] = 3; (-> b[0] == 3) 3 == b[0] (-> true) b[1] = b[0] + 4; (-> b[1] == 7) b[1024] = 5; (-> FEHLER!!) b.length() (-> 1024) mh@informatik.tu-cottbus.de 5 / 12 mh@informatik.tu-cottbus.de 6 / 12
4. Strings ( Array s of char) : String gruss = new String ("hallo"); String gruss1 = "hallo"; String gruss2 = new String(gruss); "...", "42", "hallo\n", "\ " length(), + (Konkatenation), charat() (sei int x = 10) gruss + "xyz" (-> halloxyz ) gruss + 42 (-> "hallo42") gruss + x + true + "x" (-> hallo10truex) gruss.length() == 5 gruss2.charat(4) (-> o) 5. Anweisungen a) Zuweisung x = 3; y = x + 123; b) if-anweisung if (x == 3) { else { alternative, optional c) switch-anweisung byte b;... switch (b) { case 1: break; case -100: break;... default: Achtung: Immer break & default verwenden!!! Mögliche Typen für die Auswahl: boolean, byte, char, short, int, long mh@informatik.tu-cottbus.de 7 / 12 mh@informatik.tu-cottbus.de 8 / 12
d) while-schleife int i = 10; while (i < 0) { e) do-while-schleife do { while (i > 10) f) for-schleife (Zählschleife) for (int i = 0; i < 10; i++) { entspricht int i = 0; while (i < 10) { i++; 6. Das erste Java-Programm Datei Hello.java public class Hello { // Kommentar: // Jedes Javaprogramm besteht aus einer // Ansammlung von Klassen, wobei jede // Klasse in ihrer eigenen Datei // untergebracht ist. Diese Datei muss // ebenso wie die Klasse heissen! public static void main (String argv[]) { /* Noch ein Kommentar: Diese Klasse enthaelt eine Funktion main() mit genau diesem Kopf! Diese Funktion ist der Einstiegspunkt in das Programm. */ String s = hello ; System.out.println(s + World\n ); // Ausgabe System.exit(0); // Termination mit Rueckgabewert 0 mh@informatik.tu-cottbus.de 9 / 12 mh@informatik.tu-cottbus.de 10 / 12
7. Java-Programm Max Problem: Eingelesen werden k Zahlen, ausgegeben wird deren Maximum! import dssz.io.stdin; public class Max { private static int maximum(int[] numbers) {... public static void main(string argv[]) { int i = 0; int k = 0; int[] numbers; stdin eingabe = new stdin(); System.out.print( Wieviel Zahlen: ); k = eingabe.getintnumber(); numbers = new int[k]; for(i = 0; i < k; i++) { numbers[i] = eingabe.getintnumber(); System.out.println( Maximum ist + maximum(numbers)); System.exit(0); private static int maximum(int[] numbers) { int i = 0;// Zaehlvariable int max = 0;// Ergebnisvariable if(numbers.length == 0) { max = 0; else if (numbers.length == 1) { max = numbers[0]; else { max = numbers[0]; for(i = 1; i < numbers.length; i++) { if(max < numbers[i]) { max = numbers[i]; return max; mh@informatik.tu-cottbus.de 11 / 12 mh@informatik.tu-cottbus.de 12 / 12