Presentation is loading. Please wait.

Presentation is loading. Please wait.

Language Features for JDK7 Neal Gafter Joshua Bloch Google.

Similar presentations


Presentation on theme: "Language Features for JDK7 Neal Gafter Joshua Bloch Google."— Presentation transcript:

1

2 Language Features for JDK7 Neal Gafter Joshua Bloch Google

3 www.javapolis.com A slate of possible small language changes Improved type inference Enum comparison String switch Chained invocations & Extension methods Improved catch clauses Array notation for Map, List Typedef Serialization annotations Self type Properties

4 www.javapolis.com Improved type inference: constructors Today: Map > anagrams = new HashMap >();

5 www.javapolis.com Improved type inference: constructors Proposed: Map > anagrams = new HashMap<>();

6 www.javapolis.com Improved type inference: argument positions Today: public Set emptySet() { … } void timeWaitsFor(Set people) { … } // * Won't compile! timeWaitsFor(Collections.emptySet());

7 www.javapolis.com Improved type inference: argument positions Today: public Set emptySet() { … } void timeWaitsFor(Set people) { … } // OK timeWaitsFor(Collections. emptySet());

8 www.javapolis.com Improved type inference: argument positions Proposed: public Set emptySet() { … } void timeWaitsFor(Set people) { … } // OK timeWaitsFor(Collections.emptySet());

9 www.javapolis.com Enum comparison Today enum Size { SMALL, MEDIUM, LARGE } if (mySize.compareTo(yourSize) >= 0) System.out.println( You can wear my pants. );

10 www.javapolis.com Enum comparison Proposed enum Size { SMALL, MEDIUM, LARGE } if (mySize > yourSize) System.out.println( You can wear my pants. );

11 www.javapolis.com String switch Today static boolean booleanFromString(String s) { if (s.equals("true")) { return true; } else if (s.equals("false")) { return false; } else { throw new IllegalArgumentException(s); }

12 www.javapolis.com String switch Proposed static boolean booleanFromString(String s) { switch(s) { case "true": return true; case "false": return false; default: throw new IllegalArgumentException(s); }

13 www.javapolis.com Chained invocations and Extension Methods Enables declarative style: List strings =...; strings.filter(isCountryName) // could be a closure.sort().uniq().each(printString); // ditto

14 www.javapolis.com Extension methods Today import java.util.Collections; List list = … ; Collections.sort(list);

15 www.javapolis.com Extension methods Proposed import static java.util.Collections.sort; List list = … ; list.sort();

16 www.javapolis.com Chained invocations Today class Builder { void setSomething(Something x) { … } void setOther(Other x) { … } Thing result() { … } } Builder builder = new Builder(); builder.setSomething(something); builder.setOther(other); Thing thing = builder.result();

17 www.javapolis.com Chained invocations Proposed class Builder { void setSomething(Something x) { … } void setOther(Other x) { … } Thing result() { … } } Thing thing = new Builder().setSomething(something).setOther(other).result();

18 www.javapolis.com Chained invocations and Extension Methods Enables declarative style: List strings =...; strings.filter(isCountryName) // could be a closure.sort().uniq().each(printString); // ditto

19 www.javapolis.com Improved catch clauses: catching multiple types Today: try { return klass.newInstance(); } catch (InstantiationException e) { throw new AssertionError(e); } catch (IllegalAccessException e) { throw new AssertionError(e); }

20 www.javapolis.com Improved catch clauses: catching multiple types Proposed: try { return klass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new AssertionError(e); }

21 www.javapolis.com Improved catch clauses: rethrown exceptions Today: try { doable.doit(); // Throws several types } catch (Throwable ex) { logger.log(ex); throw ex; // Error: Throwable not declared }

22 www.javapolis.com Improved catch clauses: rethrown exceptions Proposed: try { doable.doit(); // Throws several types } catch (final Throwable ex) { logger.log(ex); throw ex; // OK: Throws the same several types }

23 www.javapolis.com Array notation for Map, List Today: void swap(List list, int i, int j) { String s1 = list.get(i); list.set(i, list.get(j)); list.set(j, s1); }

24 www.javapolis.com Array notation for Map, List Proposed: void swap(List list, int i, int j) { String s1 = list[i]; list[i] = list[j]; list[j] = s1; }

25 www.javapolis.com Array notation for Map, List Today: Map cache = … ; Output cachedComputation(Input in) { Output out = cache.get(in); if (out == null) { out = computation(input); cache.put(in, out); } return out; }

26 www.javapolis.com Array notation for Map, List Proposed: Map cache = … ; Output cachedComputation(Input in) { Output out = cache[in]; if (out == null) { out = computation(input); cache[in] = out; } return out; }

27 www.javapolis.com Self Type Today class Buffer { Buffer flip() { … } Buffer position(int newPos) { … } Buffer limit(int newLimit) { … } } class ByteBuffer extends Buffer { ByteBuffer put(byte data) { … } }

28 www.javapolis.com Self Type Today ByteBuffer buf =...; buf.flip().position(12); // OK buf.flip().put(12); // Error

29 www.javapolis.com Self Type Proposed class Buffer { this flip() { … } this position(int newPos) { … } this limit(int newLimit) { … } } class ByteBuffer extends Buffer { this put(byte data) { … } }

30 www.javapolis.com Typedef import java.util.Map as MyProperties; import java.util.Map as Lookup ; // if we add BGGA function types import { double => double } as DoubleFcn; Alternatively static class MyProperties = Map ; static class Lookup = Map ; // if we add BGGA function types static class DoubleFcn = { double => double };

31 Q&A View JavaPolis talks @ www.parleys.com

32 Thank you for your attention


Download ppt "Language Features for JDK7 Neal Gafter Joshua Bloch Google."

Similar presentations


Ads by Google