Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010...

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

Transcript of Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010...

Page 1: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 1

Neues ab JDK 1.5

Praktikum aus Softwareentwicklung 2

Page 2: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 2

Java 5 - Tiger

• New Features– Timeline, Überblick

• Java Spracherweiterungen– Enums, For Loop, Generics, ...

• Java API– StringBuilder, Formatter

• Sonstiges

Page 3: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 3

Java SE Plattform VersionenTimeline

Versionen, Stand März 2010:

• JDK 1.4.2_12

• Java SE 5 Update 15 (Tiger)

• Java SE 6 Update 18

• Java SE 7 voraussichtlich 2010 (Early Access verfügbar)

Page 4: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 4

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

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

• Java Virtual Machine– Performanz

Java 5 Erweiterungen

Page 5: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 5

Java 5

• New Features– Timeline, Überblick

• Java Spracherweiterungen– Enums, For Loop, Generics, …

• Java API– StringBuilder, Formatter

• Sonstiges

Page 6: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 6

• Konstante als Aufzählungstypen– public final static int

FORM_FIELD_COLOR_BLUE = 0;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

Java SpracherweiterungenTypesafe Enumerations (1/4)

Page 7: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 7

class Form {

class Field {

enum Color { BLUE, RED, GREEN }; …

Color fontColor = BLUE; …

• Namensbereiche durch innere Typen● Form.Field.Color

• Enums entsprechen Klassen● BLUE,RED,GREEN - globale Objekte vom Typ Color

● Können Konstruktoren und Methoden besitzen

Java SpracherweiterungenTypesafe Enumerations (2/4)

Page 8: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 8

enum Color {

BLUE (0x00,0x00,0xFF),

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

Java SpracherweiterungenTypesafe Enumerations (3/4)

Page 9: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 9

• Vergleich mit == möglich– Color c = Color.RED;

if (c == Color.RED) {…}

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

Java SpracherweiterungenTypesafe Enumerations (4/4)

Page 10: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 10

Java Spracherweiterungen Autoboxing/Unboxing (1/2)

• Automatische Umwandlung von primitiven Datentypen in ihre Wrapper Klassen:– Integer > int > Integer – Double > double > Double

• Ohne Autoboxing– int x = 123, z;

Integer y;

y = Integer.valueOf(x);

z = y.intValue();

• Mit Autoboxing– y = x;

z = y;

Page 11: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 11

• Umwandlung übernimmt der Compiler• Achtung, NullPointerExceptions möglich:

– Integer a = null;

int b = a; // Bumm!

• Unboxing NullPointerException weil:– Integer a = null;

int b = a.valueOf();

Java Spracherweiterungen Autoboxing/Unboxing (2/2)

Page 12: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 12

• Spezielle Typ-Parameter für Klassen/Interfaces und Methoden

– Typsicherheit– Vermeidung von type-casts

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

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

• Lesen als: „Observable vom Typ T“

Java Spracherweiterungen Generics (1/4)

Page 13: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 13

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

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

• ArrayList<String> list = new ArrayList<String>();

String s = list.get(0);

• HashMap<Integer,String> map;int userID = 123;

map.put(userID, "hans im glück");

Java Spracherweiterungen Generics (2/4)

Page 14: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 14

• Typisieren von Methoden, zum Beispiel ein Setter für Observer:– Parameter soll mindestens vom Typ PasswordObserver sein, und

– Parameter soll das Interface Observable<User> implementieren

• <T extends PasswordObserver &

Observable<User>>

void set(T observer) {…}

Java Spracherweiterungen Generics (3/4)

Page 15: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 15

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

• Wildcards– Upper BoundfindUsers(

Collection<? extends User> users)

– Lower BoundfindUsers(

Collection<? super Administrator> users)

Java Spracherweiterungen Generics (4/4)

Page 16: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 16

Java Spracherweiterungen Enhanced For Loop (1/3)

try {

ArrayList users; users.add(aUser); …

Iterator it = users.iterator();

while (it.hasNext()) {

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

…;

}

} catch (ClassCastException ce) {

ce.printStackTrace();

}

Page 17: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 17

• Compiler übernimmt– Generieren des Iterators– Lesen vom Iterator– Typ Überprüfung– Prüfung auf Listenende

• ArrayList<User> users;

for (User u : users) { …

}

• Nicht mehr notwendig:try ... catch(ClassCastException e)...

Java Spracherweiterungen Enhanced For Loop (2/3)

Page 18: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 18

Java Spracherweiterungen Enhanced For Loop (3/3)

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

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

}

• Wird übersetzt zu: for (int g=0; g < numbs.length; ++g) {

int x = numbs[g]; System.out.println(x);

}

Page 19: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 19

• Metainformation im Programmkode• Verarbeitung durch:

– Compiler (werden vom Compiler entfernt)– Tools (werden von der VM entfernt)– Zur Laufzeit

• Package java.lang.annotation• Beispiele:

– JAX-WS (SOAP Stack von Sun)– JUnit 4

Java Spracherweiterungen Annotation

Page 20: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 20

• Import von statisch definierten– Variablen– Klassen– Enums

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

– import static java.lang.Math.*;

Java Spracherweiterungen Static Import (1/2)

Page 21: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 21

• Beispiel:import java.lang.Math;

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);

Java Spracherweiterungen Static Import (2/2)

Page 22: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 22

Java Spracherweiterungen Varargs (1/2)

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

• Arrays mit unterschiedlichen Typen nur als Object• Funktionalität printf wie in C/C++ ist nicht möglich // Bisher

public static void printem(String x[]) { for (int t=0;t<x.length;++t) {

System.out.println(x[t]);

}

}

...

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

Page 23: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 23

Java Spracherweiterungen Varargs (2/2)

// Neu

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

for (String s : x) {

System.out.println(x);

}

}

public static void main(String args[]) {

// JDK1.5 style

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

...

}

Page 24: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 24

Java 5

• New Features– Timeline, Überblick

• Java Spracherweiterungen– Enums, For Loop, Generics, ...

• Java API– StringBuilder, Formatter

• Sonstiges

Page 25: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 25

Java API News StringBuilder

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

– Zum Verketten von Strings

• 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

Page 26: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 26

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

– Thread.getAllStackTraces();

– Verwendung für Statistiken, Ausgabeformatierung…

• Standard Implementierung für unbehandelte Exceptions

– Thread.setDefaultUncaughtExceptionHandler(

myDefaultHandler);

– Verwendung für spezifisches Logging– Keine Exceptions werden "übersehen"

Java API News Exception Handling

Page 27: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 27

Java API News Formatted Output (1/2)

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

• 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);

Page 28: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 28

Java API News Formatted Output (2/2)

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

• Konstruktoren für flexible Ausgabeziele und Zeichenkodierungen

– Streams, Files, java.lang.Appendable

• Beispiel FileOutputStream out; …

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

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

out.close();

Page 29: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 29

• Text Scanner– java.util.Scanner – Parser für primitive Typen und Strings

• 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

Java API News Formatted Input

Page 30: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 30

Java API News Management (1/2)

import java.lang.management.*;

import java.util.*;

import javax.management.*;

public class MemTest {

public static void main(String args[]) {

List<java.lang.management.MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();

for(MemoryPoolMXBean p: pools) {

System.out.println("Memory type=" + p.getType());

System.out.println("Memory usage=" + p.getUsage());

...

• Interface für Zugriff auf Daten für Monitoring

Page 31: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 31

• Monitor Tool: jconsole• Aktivierung für JVM

-Dcom.sun.management.jmxremote

-Dcom.sun.management.jmxremote.authenticate=false-Dcom.sun.management.jmxremote.ssl=false-Dcom.sun.management.jmxremote.port=9999

Java API News Management (2/2)

Page 32: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 32

Java 5• New Features

– Timeline, Überblick• Java Spracherweiterungen

– Enums, For Loop, Generics, ...• Java API

– StringBuilder, Formatter• Sonstiges

Page 33: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 33

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

• Class Data Sharing– 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

Java 5 ErweiterungenJava Virtual Machine

Page 34: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 34

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

• Internationalisierung– Unicode 4.0, Vietnam Locale

• Formatter (java.util)– Interpreter Klasse für printf-Style Format Strings

String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", calendar);

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

Java 5 Erweiterungen Core Libraries (1/2)

Page 35: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 35

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

• Monitoring, Managment, Instrumentation • Concurrency Utilities• Bit Manipulation Operations• http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html

Java 5 Erweiterungen Core Libraries (2/2)

Page 36: Neues ab JDK 1 · 2010. 3. 16. · Praktikum aus Softwareentwicklung 2 Java Praktikum – SS 2010 – Gerald.Ehmayer@borland.com 3 Java SE Plattform Versionen Timeline Versionen,

Praktikum aus Softwareentwicklung 2

Java Praktikum – SS 2010 – [email protected] 36

Ende der 2. Übung