38
Praktikum aus Softwareentwicklung 2 Neues ab JDK 1.5 Java Praktikum – SS 2008 – [email protected] 1 Praktikum aus Softwareentwicklung 2

Neues ab JDK 1 - Johannes Kepler University LinzPraktikum aus Softwareentwicklung 2 Neues ab JDK 1.5 Java Praktikum –SS 2008 –[email protected] 1 Praktikum aus Softwareentwicklung

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • Praktikum aus Softwareentwicklung 2

    Neues ab JDK 1.5

    Java Praktikum – SS 2008 – [email protected] 1

    Praktikum aus Softwareentwicklung 2

  • Praktikum aus Softwareentwicklung 2

    Java 5 - Tiger

    • New Features

    – Timeline, Überblick

    • Java Spracherweiterungen

    – Enums, For Loop, ..

    Java Praktikum – SS 2008 – [email protected] 2

    – Enums, For Loop, ..

    • Java API

    – StringBuilder, Formatter

    • Sonstiges

  • Praktikum aus Softwareentwicklung 2

    J2SE Plattform VersionenTimeline

    Java Praktikum – SS 2008 – [email protected] 3

    Aktuelle Versionen:

    • JDK 1.4.2_12

    • JDK 5 Update 15

    • JDK 6 Update 5

  • Praktikum aus Softwareentwicklung 2

    Jsdk1.5 Übersicht

    Java Praktikum – SS 2008 – [email protected] 4

  • Praktikum aus Softwareentwicklung 2

    • Sprache– Generics– Aufzählungstyp (enums)– Metainformation (annotations)

    Java 5 Erweiterungen

    Java Praktikum – SS 2008 – [email protected] 5

    – Metainformation (annotations)• Standard Bibliotheken– Unicode 4.0– Collections Framework– Nebenläufigkeit (Threads)– Monitoring…

    • Java Virtual Machine– Performanz

  • Praktikum aus Softwareentwicklung 2

    Java 5 - Tiger

    • New Features

    – Timeline, Überblick

    • Java Spracherweiterungen

    – Enums, For Loop, Generics, …

    Java Praktikum – SS 2008 – [email protected] 6

    – Enums, For Loop, Generics, …

    • Java API

    – StringBuilder, Formatter

    • Sonstiges

  • Praktikum aus Softwareentwicklung 2

    • Konstante als Aufzählungstypen– public final static int

    FORM_FIELD_COLOR_BLUE = 0;public final static int

    Java SpracherweiterungenTypesafe Enumerations (1/4)

    Java Praktikum – SS 2008 – [email protected] 7

    public final static int FORM_FIELD_COLOR_RED = 1;

    public final static int FORM_FIELD_COLOR_GREEN = 2;

    • Nachteile– Verwendung nicht typsicher– Umständliche Namensgebung– Erweiterung ist problematisch– Kein Informationsgehalt

  • Praktikum aus Softwareentwicklung 2

    class Form {class Field {enum Color { BLUE, RED, GREEN };…

    Java SpracherweiterungenTypesafe Enumerations (2/4)

    Java Praktikum – SS 2008 – [email protected] 8

    …Color fontColor = BLUE;…

    • Namensbereiche durch Verschachteln– Form.Field.Color

    • Enums entsprechen Klassen– BLUE,RED,GREEN entsprechen globalen Objekten vom Typ Color

    – Können Konstruktoren und Methoden besitzen

  • Praktikum aus Softwareentwicklung 2

    enum Color {BLUE (0x00,0x00,0xFF),RED (0x00,0xFF,0x00),GREEN(0xFF,0x00,0x00);

    Java SpracherweiterungenTypesafe Enumerations (3/4)

    Java Praktikum – SS 2008 – [email protected] 9

    RED (0x00,0xFF,0x00),GREEN(0xFF,0x00,0x00);int r,g,b;Color(int r, int g, int b) {…}… }

    • Initialisierung durch Konstruktor• Methoden und Members wie bei Klassen

  • Praktikum aus Softwareentwicklung 2

    • Vergleich mit == möglich– Color c = Color.RED;if (c == Color.RED) {…}switch(c) { case RED: … }

    Java SpracherweiterungenTypesafe Enumerations (4/4)

    Java Praktikum – SS 2008 – [email protected] 10

    switch(c) { case RED: … }

    • Werte-Bezeichner auch als String verfügbar– Color c = Color.valueOf("RED");if ("RED".equals(c.toString()) {…}

    • Verfügbare Werte abfragbar– Color[] all = Color.values();

    • java.lang.Enum

  • Praktikum aus Softwareentwicklung 2

    Java SpracherweiterungenAutoboxing/Unboxing (1/2)

    • Automatische Umwandlung von primitiven Datentypen in ihre Wrapper Klassen.– Integer � int � Integer

    Java Praktikum – SS 2008 – [email protected] 11

    – Integer � int � Integer

    – Double � double � Double

    – Etc...

    • Umwandeln übernimmt der Compiler

    • Achtung Nullpointer Exceptions möglich

  • Praktikum aus Softwareentwicklung 2

    • Ohne– int x = 123, z;Integer y;y = Integer.valueOf(x);

    Java SpracherweiterungenAutoboxing/Unboxing (2/2)

    Java Praktikum – SS 2008 – [email protected] 12

    y = Integer.valueOf(x);z = y.intValue();

    • Mit– y = x;z = y;

    • Unboxing NullPointerException!– Integer a = null;int b = a; // Bumm!

  • Praktikum aus Softwareentwicklung 2

    • Spezielle Typ-Parameter für Klassen/Interfaces und Methoden– Typsicherheit– Vermeidung von type-casts

    Java SpracherweiterungenGenerics (1/4)

    Java Praktikum – SS 2008 – [email protected] 13

    – Vermeidung von type-casts• interface Observable {

    public void update(Object arg){…}}

    • interface Observable {public void update(T arg){…}

    }

    • Lesen als: „Observable vom Typ T“

  • Praktikum aus Softwareentwicklung 2

    • Verwendung im Collections Framework• ArrayList list = new ArrayList();list.add("ein string…");String s = (String)list.get(0);

    Java SpracherweiterungenGenerics (2/4)

    Java Praktikum – SS 2008 – [email protected] 14

    String s = (String)list.get(0);

    • ArrayList list = new ArrayList();

    String s = list.get(0);

    • HashMap map;int userID = 123;map.put(userID, "hans im glück");

  • Praktikum aus Softwareentwicklung 2

    • Typisieren von Methoden, zum Beispiel ein Setter:– Parameter soll mindestens vom Typ

    Java SpracherweiterungenGenerics (3/4)

    Java Praktikum – SS 2008 – [email protected] 15

    – Parameter soll mindestens vom Typ PasswordObserver sein, und– Parameter soll das Interface Observable implementieren

    void set(T observer) {…}

  • Praktikum aus Softwareentwicklung 2

    • Exakter Type-check bei Parametern– findUsers(Collection users)

    • Wildcards

    Java SpracherweiterungenGenerics (4/4)

    Java Praktikum – SS 2008 – [email protected] 16

    • Wildcards–Upper BoundfindUsers(

    Collection

  • Praktikum aus Softwareentwicklung 2

    Java SpracherweiterungenEnhanced For Loop (1/3)

    try {

    ArrayList users; users.add(aUser); …

    Iterator it = users.iterator();

    Java Praktikum – SS 2008 – [email protected] 17

    Iterator it = users.iterator();

    while (it.hasNext()) {

    String u = (User) it.next();

    …;

    }

    } catch (ClassCastException ce) {

    ce.printStackTrace();

    }

  • Praktikum aus Softwareentwicklung 2

    • Compiler übernimmt– Generieren des Iterators– Lesen vom Iterator

    Java SpracherweiterungenEnhanced For Loop (2/3)

    Java Praktikum – SS 2008 – [email protected] 18

    – Lesen vom Iterator– Type-Cast– Prüfung auf Listenende

    • ArrayList users;for(User u : users) {…

    }

  • Praktikum aus Softwareentwicklung 2

    Java SpracherweiterungenEnhanced For Loop (3/3)

    • Funktioniert auch für Arraysint[] numbs = {10,20,30,40,50};

    for (int x : numbs) System.out.println(x);

    Java Praktikum – SS 2008 – [email protected] 19

    for (int x : numbs) System.out.println(x);

    Wird zufor (int g=0; g < numbs.length; ++g) {

    int x = numbs[g];

    ...

  • Praktikum aus Softwareentwicklung 2

    • Metainformation im Programmkode• Verarbeitung durch– Compiler

    Java SpracherweiterungenAnnotation

    Java Praktikum – SS 2008 – [email protected] 20

    – Compiler– Tools– Zur Laufzeit

    • Package java.lang.annotation• Beispiel: – JAX-WS (SOAP Stack von Sun)

  • Praktikum aus Softwareentwicklung 2

    • Import von statisch definierten–Variablen

    –Klassen

    Java SpracherweiterungenStatic Import (1/2)

    Java Praktikum – SS 2008 – [email protected] 21

    –Klassen

    –Enums

    • Analog zu Package Import– import static java.lang.Math.PI;

    – import static java.lang.Math.*;

  • Praktikum aus Softwareentwicklung 2

    • Beispiel:import java.lang.Math;

    double x = Math.PI;

    Java SpracherweiterungenStatic Import (2/2)

    Java Praktikum – SS 2008 – [email protected] 22

    double x = Math.PI;

    double y = Math.cos(Math.PI/2d – x);

    import static java.lang.Math.*;

    double x = PI;

    double y = cos(PI/2d – x);

  • Praktikum aus Softwareentwicklung 2

    Java SpracherweiterungenVarargs (1/2)

    • Um an eine Methode eine variable Anzahl von Parametern zu übergeben, muss man ein Array verwenden

    • Arrays anlegen und füllen ist mühsam• Funktionalität printf wie in C/C++ ist nicht möglich

    // Bisher

    Java Praktikum – SS 2008 – [email protected] 23

    // Bisher

    public static void printem(String x[]) {

    for (int t=0;t

  • Praktikum aus Softwareentwicklung 2

    Java SpracherweiterungenVarargs (2/2)

    // Neu

    public static void printem(String... x) {

    for (String s : x) {

    System.out.println(x);

    Java Praktikum – SS 2008 – [email protected] 24

    System.out.println(x);

    }

    }

    public static void main(String args[]) {

    // JDK1.5 style

    printem("bob","fred","joe");

    //JDK1.4 style

    printem(new String[] {"bob","fred","joe"});

    }

  • Praktikum aus Softwareentwicklung 2

    Java 5 - Tiger

    • New Features

    – Timeline, Überblick

    • Java Spracherweiterungen

    – Enums, For Loop, ..

    Java Praktikum – SS 2008 – [email protected] 25

    – Enums, For Loop, ..

    • Java API

    – StringBuilder, Formatter

    • Sonstiges

  • Praktikum aus Softwareentwicklung 2

    Java API NewsStringBuilder

    • StringBuilder als Alternative zu StringBuffer– java.lang.StringBuilder

    – Zum Verketten von Strings

    • StringBuilder

    Java Praktikum – SS 2008 – [email protected] 26

    • StringBuilder– Eigenschaften und Schnittstelle wie StringBuffer

    StringBuilder sbuilder = new StringBuilder();

    sbuilder.append("Plz.: ").append(4040)…;

    • Nicht thread safe, dafür bessere Performanz!– StringBuffer NICHT mehr verwenden• Ausnahme: gleichzeitige Verwendung von mehreren Threads

  • Praktikum aus Softwareentwicklung 2

    • Zugriff auf aktuellen Laufzeit-Stack– Thread.currentThread().getStackTrace();

    – Thread.getAllStackTraces();

    – Verwendung für Statistiken,

    Java API News Exception Handling

    Java Praktikum – SS 2008 – [email protected] 27

    – Verwendung für Statistiken, Ausgabeformatierung…

    • Standard Implementierung für unbehandelte Exceptions– Thread.setDefaultUncaughtExceptionHandler(

    myDefaultHandler);

    – Verwendung für spezifisches Logging–Wichtig, um keine Exceptions zu übersehen

  • Praktikum aus Softwareentwicklung 2

    Java API News Formatted Output (1/2)

    • Vorbild ist Ausgabe Formatierung von C/C++– sprintf(Format String , Daten …);

    • Beispiel:

    Java Praktikum – SS 2008 – [email protected] 28

    • Beispiel:String firstName = "Albert";

    String lastName = "Einstein";

    Date date = new Date();

    String presentation =

    "%1$td.%1$tm.%1$tY - Vorname: %2$-20s Nachname: %3$-20s";

    String.format(presentation, date, firstName, lastName);

    System.out.printf(presentation, date, firstName, lastName);

  • Praktikum aus Softwareentwicklung 2

    Java API News Formatted Output (2/2)

    • Implementiert in java.util.Formatter– Formatierungsmöglichkeiten siehe Java Doc

    • Konstruktoren für flexible Ausgabeziele

    Java Praktikum – SS 2008 – [email protected] 29

    • Konstruktoren für flexible Ausgabeziele und Zeichenkodierungen– Streams, Files, java.lang.Appendable

    • BeispielFileOutputStream out; …

    Formatter formatter = new Formatter(out,"UTF-8");

    formatter.format("PI = %12.10f", Math.PI);

    out.close();

  • Praktikum aus Softwareentwicklung 2

    • Text Scanner– java.util.Scanner

    – Parser für primitive Typen und Strings• Konstruktoren für flexible Quellen und

    Java API News Formatted Input

    Java Praktikum – SS 2008 – [email protected] 30

    • Konstruktoren für flexible Quellen und Zeichenkodierungen– Streams, Reader, Files, Strings– Scanner s = new Scanner(Quelle);int val = s.nextInt(); …s.close();

    • Vergleiche auch– String[] tokens =

    "aaa bbb ccc".split(regular expression);– Obsolet: java.util.StringTokenizer

  • Praktikum aus Softwareentwicklung 2

    Java API News Management (1/2)

    import java.lang.management.*;

    • Interface für Zugriff und Bereitstellung von Daten für Monitoring

    Java Praktikum – SS 2008 – [email protected] 31

    import java.lang.management.*;import java.util.*;

    import javax.management.*;

    public class MemTest {

    public static void main(String args[]) {

    List pools =ManagementFactory.getMemoryPoolMXBeans();

    for(MemoryPoolMXBean p: pools) {System.out.println("Memory type=" + p.getType());System.out.println("Memory usage=" + p.getUsage());

    }

    }

    }

  • Praktikum aus Softwareentwicklung 2

    • Monitor Tool: jconsole

    • Aktivierung für JVM-Dcom.sun.management.jmxremote

    Java API News Management (2/2)

    Java Praktikum – SS 2008 – [email protected] 32

    -Dcom.sun.management.jmxremote

    -Dcom.sun.management.jmxremote.authenticate=false

    -Dcom.sun.management.jmxremote.ssl=false

    -Dcom.sun.management.jmxremote.port=9999

  • Praktikum aus Softwareentwicklung 2

    Java 5 - Tiger

    • New Features

    – Timeline, Überblick

    • Java Spracherweiterungen

    – Enums, For Loop, ..

    Java Praktikum – SS 2008 – [email protected] 33

    – Enums, For Loop, ..

    • Java API

    – StringBuilder, Formatter

    • Sonstiges

  • Praktikum aus Softwareentwicklung 2

    • Java Heap Self Tuning– (Selbst)-Einstellung des Heap wurde optimiert (Performance und Speicherverbrauch)

    • Class Data Sharing– Soll Startzeit bei kleinen Applikationen reduzieren

    Java 5 ErweiterungenJava Virtual Machine

    Java Praktikum – SS 2008 – [email protected] 34

    – Soll Startzeit bei kleinen Applikationen reduzieren– Teile der Standardbibliotheken werden in ein Memory-Mapped-File ausgelagert

    – jre\bin\client\classes.jsa

    – MMF wird zwischen mehreren VM‘s geteilt

    • Garbage Collector Ergonomics– Parallele Garbage Collector verbessert– Big Size Verbesserungen, besseres adaptives Verhalten

  • Praktikum aus Softwareentwicklung 2

    • Network– Timeout (Protocoll Handler), InetAddress ping, Framework für File Caching

    • Internationalisierung

    Java 5 Erweiterungen Core Libraries (1/2)

    Java Praktikum – SS 2008 – [email protected] 35

    • Internationalisierung– Unicode 4.0, Vietnam Locale

    • Formatter (java.util)– Interpreter Klasse für printf-Style Format StringsString s = String.format("Duke's Birthday: %1$tm

    %1$te,%1$tY", calendar);

    -> s == "Duke's Birthday: May 23, 1995"

  • Praktikum aus Softwareentwicklung 2

    • Collections– 3 neue Interfaces (Queue, BlockingQueue, ConcurrentMap)

    – Einige neue Algorithmen

    • Monitoring, Managment, Instrumentation

    Java 5 Erweiterungen Core Libraries (2/2)

    Java Praktikum – SS 2008 – [email protected] 36

    • Monitoring, Managment, Instrumentation

    • Concurrency Utilities

    • Bit Manipulation Operations

    • http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

  • Praktikum aus Softwareentwicklung 2

    Links

    http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

    http://java.sun.com/developer/technicalArticles/releases/j2se15/

    http://java.sun.com/j2se/1.5.0/

    Java Praktikum – SS 2008 – [email protected] 37

    http://www.ibm.com/developerworks/forums/dw_jforums.jspa

    http://java.sun.com/docs/books/tutorial/

  • Praktikum aus Softwareentwicklung 2

    Ende der 2. Übung

    Java Praktikum – SS 2008 – [email protected] 38