Lucky Seven Kleine Sprachnderungen in Java 7 Michael Wiedeking - - PowerPoint PPT Presentation

lucky seven
SMART_READER_LITE
LIVE PREVIEW

Lucky Seven Kleine Sprachnderungen in Java 7 Michael Wiedeking - - PowerPoint PPT Presentation

Lucky Seven Kleine Sprachnderungen in Java 7 Michael Wiedeking MATHEMA Software GmbH Agenda Things we wont talk about Things that may come Q & A Herbstcampus 2009 Java 7 Things we wont talk about Java Modules


slide-1
SLIDE 1

Lucky Seven

Kleine Sprachänderungen in Java 7

Michael Wiedeking

MATHEMA Software GmbH

slide-2
SLIDE 2

Herbstcampus 2009 – Java 7

Agenda

  • Things we won’t talk about
  • Things that may come
  • Q & A
slide-3
SLIDE 3

Herbstcampus 2009 – Java 7

Things we won’t talk about

  • Java Modules
  • Annotations of Types
  • Sorry, no Closures
slide-4
SLIDE 4

Herbstcampus 2009 – Java 7

But: Remember Galadriel’s Mirror

A basin filled with water in which one may see visions of the past, present and future, appearing in The Lord of the Rings. Galadriel, an Elf ruler of Lothlórien, invites the story's hero, Frodo Baggins, to look into it. Galadriel cannot predict what the mirror will show and does not guarantee that its visions will come to pass.

slide-5
SLIDE 5

Herbstcampus 2009 – Java 7

Fun with Java – Integer Literals

Multiples of 37 int[ ] table = { 000, 037, 074, 111, 148, 185, 222, 259, 296, 333, 370 };

slide-6
SLIDE 6

Herbstcampus 2009 – Java 7

Integer Literals

  • Before
  • int i = 0xCAFE;
  • int l = 0xBABEL;
  • After
  • int i = 0xCAFE_BABE;
  • int j = 0b0110_1100_0111_0101;
  • long l = 123_456_789_012L;
  • byte b = 0xFFu;
slide-7
SLIDE 7

Herbstcampus 2009 – Java 7

Indexing

  • Before

List<String> list = …; String firstElement = list.get(0); Map<Integer, String> map = new HashMap<>(); map.put(Integer.valueOf(1), "One”);

  • After

List<String> list = …; String firstElement = list[0]; Map<Integer, String> map = new HashMap<>(); map[Integer.valueOf(1)] = "One";

slide-8
SLIDE 8

Herbstcampus 2009 – Java 7

Indexing

// New superinterfaces to indicate syntax? interface GettableFromInt<E> { E get(int); } interface Gettable<V> { V get(Object); } interface Settable<E> { E set(int, E); } interface Puttable<K, V> { V put(K, V); }

slide-9
SLIDE 9

Herbstcampus 2009 – Java 7

Indexing

java.util.List<E> extends GettableFromInt<E>, Settable<E>, … java.util.Map<K, V> extends Gettable<V>, Puttable(K, V), …

slide-10
SLIDE 10

Herbstcampus 2009 – Java 7

Collection Literals

  • Before

List<String> list = Collections.asList(new String[ ] {"a", "b", "c”});

  • After

List<String> list = ["a", "b", "c"]; String firstElement = list[0]; Map<Integer, String> map = {1 : "One"};

slide-11
SLIDE 11

Herbstcampus 2009 – Java 7

Better Diagnostics

  • Before

interface List<E> {} class Test { <T> void merge(List<T> l1, List<T> l2) {} void test(List<? extends Test> list) { merge(list, list); } }

Test.java:6: <T>merge(List<T>,List<T>) in Test cannot be applied to (List<capture#339 of ? extends Test>,List<capture#768 of ? extends Test>) merge(list, list); ^ 1 error

slide-12
SLIDE 12

Herbstcampus 2009 – Java 7

Better Diagnostics

  • After

Test.java:6: method merge in class Test cannot be applied to given types merge(list, list); ^ required: List<T>,List<T> found: List<CAP#1>,List<CAP#2> where T is a type-variable: T extends Object declared in method <T>merge(List<T>,List<T>) where CAP#1,CAP#2 are fresh type-variables: CAP#1 extends Test from capture of ? extends Test CAP#2 ex1 error

slide-13
SLIDE 13

Herbstcampus 2009 – Java 7

Type Literals

> Before Type type = String.class > After Type<List<String>> x = List<String>.class;

slide-14
SLIDE 14

Herbstcampus 2009 – Java 7

Type Inference (1/2)

> Before Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); > After Map<String, List<String>> anagrams = new HashMap<>();

slide-15
SLIDE 15

Herbstcampus 2009 – Java 7

Type Inference (2/2)

> Before timeWaitsFor(Collections.<Man>emptySet()); > After timeWaitsFor(Collections.emptySet());

slide-16
SLIDE 16

Herbstcampus 2009 – Java 7

Automatic Resource Block Management

> Before

try { InputStream in = … OutputStream out = … ... // Perform action with in and out } catch (Exception e) { in.close();

  • ut.close();

}

slide-17
SLIDE 17

Herbstcampus 2009 – Java 7

Automatic Resource Block Management

> Before

try { … } catch (Exception e) { if (in != null) { in.close(); } if (out != null) {

  • ut.close();

} }

slide-18
SLIDE 18

Herbstcampus 2009 – Java 7

Automatic Resource Block Management

> Before if (in != null) { try { in.close(); } catch (IOException ioe) { // ??? } } if (out != null) { // … out …; }

slide-19
SLIDE 19

Herbstcampus 2009 – Java 7

Automatic Resource Block Management

> After // Start a block using two resources, which will // automatically be cleaned up when the block // exits scope do (InputStream in = …; OutputStream out = …) { ... // Perform action with in and out }

slide-20
SLIDE 20

Herbstcampus 2009 – Java 7

Language Level XML Support

> Before – / – > After elt.appendChild( <muppet> <name>Kermit</name> </muppet> );

slide-21
SLIDE 21

Herbstcampus 2009 – Java 7

JavaBean Property Support

  • Before

private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }

  • After

public property String name; a->Name = b->Name;

slide-22
SLIDE 22

Herbstcampus 2009 – Java 7

Operator Overloading

> Before BigDecimal a = new BigDecimal(“123…890“); BigDecimal b = … BigDecimal c = … a = a.mul(b).add(c); > After BigDecimal a = … BigDecimal b = … BigDecimal c = … b = a * b + c;

slide-23
SLIDE 23

Herbstcampus 2009 – Java 7

Strings in switch Statement

  • Before

static boolean booleanFromString(String s) { if (s.equals("true")) { return true; } else if (s.equals("false")) { return false; } else { throw new IllegalArgumentException(s); } }

slide-24
SLIDE 24

Herbstcampus 2009 – Java 7

Strings in switch Statement

  • After

static boolean booleanFromString(String s) { switch(s) { case "true": return true; case "false": return false; default: throw new IllegalArgumentException(s); } }

slide-25
SLIDE 25

Herbstcampus 2009 – Java 7

Comparisons for Enums

> Before

– / –

> After

boolean isRoyalty(Rank r) { return r >= Rank.JACK && r != Rank.ACE; }

slide-26
SLIDE 26

Herbstcampus 2009 – Java 7

Chained Invocation

  • Before

Factory factory = new Factory(); factory.setSomething(something); factory.setOther(other); Thing thing = factory.result();

  • After

class Factory { void setSomething(Something something) { ... } void setOther(Other other) { ... } Thing result() { ... } } Thing thing = new Factory() .setSomething(something) .setOther(other) .result();

slide-27
SLIDE 27

Herbstcampus 2009 – Java 7

Extension Methods

> Before – / – > After import static java.util.Collections.sort; List<String> list = ...; list.sort(); // looks like call to List.sort(), but really is call to static method Collections.sort().

slide-28
SLIDE 28

Herbstcampus 2009 – Java 7

Enhanced Null Handling

> Before public String getPostcode(Person person) { if (person != null) { Address address = person.getAddress(); if (address != null) { return address.getPostcode(); } } return null; }

slide-29
SLIDE 29

Herbstcampus 2009 – Java 7

Enhanced Null Handling

> After public String getPostcode(Person person) { return person?.getAddress()?.getPostcode(); }

slide-30
SLIDE 30

Herbstcampus 2009 – Java 7

Do you know what this might be?

?:

slide-31
SLIDE 31

Herbstcampus 2009 – Java 7

Maybe this helps?

?:

slide-32
SLIDE 32

Herbstcampus 2009 – Java 7

Do you know this man?

slide-33
SLIDE 33

Herbstcampus 2009 – Java 7

Null Safe Operator (Elvis-Operator)

  • Before

String s = returnImagePathOrNull(id); if (s == null) { s = “ghost.gif”; }

  • After

String s = returnImagePathOrNull(id) ?: “ghost.gif”;

slide-34
SLIDE 34

Herbstcampus 2009 – Java 7

Enhanced Null Handling

> Before String str = getStringMayBeNull(); str = (str == null ? "" : str); > After String str = getStringMayBeNull() ?: "";

slide-35
SLIDE 35

Herbstcampus 2009 – Java 7

Improved catch Clause

> Before try { return clazz.newInstance(); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); }

slide-36
SLIDE 36

Herbstcampus 2009 – Java 7

Improved catch Clause

> After try {

return clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new AssertionError(e); }

slide-37
SLIDE 37

Herbstcampus 2009 – Java 7

Improved catch Clause

> After try { doable.doIt(); } catch (final Throwable ex) { logger.log(ex); // surrounding method can declare only // checked Exceptions thrown by doIt() throw ex; }

slide-38
SLIDE 38

Herbstcampus 2009 – Java 7

Types and Generics

  • Reified Generics

NO

  • Type Literals

NO

  • JSR 308 Annotations on Java Types

YES

  • Type Inference

YES

slide-39
SLIDE 39

Herbstcampus 2009 – Java 7

Language Proposals

  • Closures

NO

  • Automatic Resource Management Blocks NO
  • Language level XML support

NO

  • JavaBean property support

NO

slide-40
SLIDE 40

Herbstcampus 2009 – Java 7

Miscellaneous Language

  • BigDecimal operator support

NO

  • Strings in switch statements

HMM

  • Comparisons for Enums

HMM

  • Chained invocations

NO?

  • Extension methods

NO?

  • Improved catch

YES

  • Null-safe handling

YES

slide-41
SLIDE 41

Herbstcampus 2009 – Java 7

Thank you!

Q & A

slide-42
SLIDE 42

Vielen Dank! Michael Wiedeking

MATHEMA Software GmbH