Programmieren Tutorium 12

Typen, Objekte und Operatoren

Gliederung

  • Organisatorisches
  • Vertiefung
    • Primitives
    • Operatoren
    • Objekte und Klassen
  • Java Class Library
  • Coding Styles

Organisatorisches

  • Disclaimer-Abgabe verlängert
    • bis zum 11.11.2013
  • Im Praktomat anmelden!

Primitives

  • Wrapper-Klassen
  • Spezielle Werte

Wrapper-Klassen

Primitive Wrapper-Klasse
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Mum said I was special…

System.out.println(Integer.MIN_VALUE == Integer.MAX_VALUE + 1);
System.out.println(0.0/0.0);
System.out.println(1/0);
System.out.println(1/0.0);
System.out.println(Double.POSITIVE_INFINITY/0.0);
  • Was passiert?
  • Erwartete Ausgabe?

Operatoren

  • Logische Operatoren
  • Präzedenzen
  • Casts

Bitwise, Logical, Short-Circuit

int x = 1;
int y = 2;
int z = 3;

System.out.println(x & y);
System.out.println(x | y);

System.out.println((x == y) || (x < z));
System.out.println((x < z)  || (x == y));
System.out.println((x < z)  |  (x == y));

System.out.println((x == y) && (x < z));
System.out.println((x < z)  && (x == y));
System.out.println((x < z)  &  (x == y));
  • Erwartete Ausgabe?
  • Was ist schneller?

Präzedenzen

int x = 1;
int y = 2;
int z = 3;

System.out.println((x != y) && (x < z));
System.out.println(x != y && x < z);

System.out.println(true | true & false);

Casts

long    myLong      = 2147483648L; // 2³¹
double  myDouble    = java.lang.Math.PI;
float   myFloat     = 2.9F;
byte    myByte      = 65;
char    myChar      = 'A';

System.out.println(myLong);
System.out.println((int) myLong);

System.out.println(myFloat);
System.out.println((int) myFloat);

System.out.println(myDouble);
System.out.println((float) myDouble);

System.out.println((byte) myChar);
System.out.println((char) myByte);
  • Erwartete Ausgabe?
  • java.lang.Math.round() für Konvertierung von Fließkomma zu Ganzzahl

Objekte und Klassen

  • Initialisierung
    • Attribute
    • Lokale Variablen
  • Referenzen
  • Enums
  • Document Management System

Initialisierung - Attribute

class Coordinates2D {
    int x;
    int y;

    public static void main(String[] args) {
        Coordinates2D c = new Coordinates2D();
        System.out.printf("c: (%d,%d)%n", c.x, c.y);
    }
}

Initialisierung - Lokale Variablen

class LocalVariables {
    public static void main(String[] args) {
        int x, y;
        System.out.printf("c: (%d,%d)%n", x, y);
    }
}
  • Lokale Variablen werden nicht automatisch initialisiert
  • Verursacht Fehler beim Kompilieren

Referenzen

Coordinates2D c1, c2;
c1 = new Coordinates2D();
c2 = c1;

c2.x = 5;
c2.y++;

c2 = new Coordinates2D();

System.out.printf("c1: (%d,%d)%n", c1.x, c1.y);
System.out.printf("c2: (%d,%d)%n", c2.x, c2.y);
  • Mehrere Referenzen auf ein und dasselbe Objekt möglich
  • Wenn keine Referenzen mehr auf ein Objekt vorhanden sind wird der Speicher freigegeben (Javas Garbage Collector)

Enums

enum Color {
    BLACK, WHITE, RED, GREEN, BLUE
}

// Assign enum value
Color background = Color.WHITE;

// Parse String eqivalent of enum member
Color userSupplied = Color.valueOf("BLUE");
  • Enums sind Mengen vordefinierter Konstanten

Jetzt seid ihr gefragt

  • Date

    Tag, Monat, Jahr

  • Author

    Vor-/Nachname, Geburtsdatum

  • Document

    Titel, Sprache, Veröffentlichungsdatum, Autor

prog12.raphinesse.de/collab

Java Class Library

  • Sehr umfangreiche Klassenbibliothek (Java 7: ~4000 Klassen)
  • Von mathematischen Funktionen bis GUI-Frameworks

Aufgaben

Beantwortet folgende Fragen mittels docs.oracle.com/javase/7

  1. Was bedeutet Deprecated bei der Methode Character.isSpace(char ch)?
  2. Wie kann man die letzten 5 Zeichen des 7 stelligen String s ausgeben?
  3. Wie welcher Funktion berechnet man am besten xⁿ?
  4. Mit welcher Klasse könnte man das Attribut Sprache unserer Document-Klasse modellieren?

Lösungen

  1. Das Element ist veraltet und sollte nicht mehr verwendet werden
  2. s.substring(2)
  3. Math.pow(x, n)
  4. java.util.Locale

Coding Style

From the Java Code Conventions

  • 80% of the lifetime cost of a piece of software goes to maintenance.
  • Hardly any software is maintained for its whole life by the original author.
  • Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly

Feierabend!

Tabs or Spaces