1 / 32

Language Features for JDK7

Language Features for JDK7. Neal Gafter Joshua Bloch Google. 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

azura
Download Presentation

Language Features for JDK7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Language Features for JDK7 Neal GafterJoshua Bloch Google

  2. 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 www.javapolis.com

  3. Improved type inference: constructors • Today: Map<String, List<String>> anagrams = new HashMap<String, List<String>>(); www.javapolis.com

  4. Improved type inference: constructors • Proposed: Map<String, List<String>> anagrams = new HashMap<>(); www.javapolis.com

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

  6. Improved type inference: argument positions • Today: public <E> Set<E> emptySet() { … } void timeWaitsFor(Set<Man> people) { … } // OK timeWaitsFor(Collections.<Man>emptySet()); www.javapolis.com

  7. Improved type inference: argument positions • Proposed: public <E> Set<E> emptySet() { … } void timeWaitsFor(Set<Man> people) { … } // OK timeWaitsFor(Collections.emptySet()); www.javapolis.com

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

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

  10. 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); } } www.javapolis.com

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

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

  13. Extension methods • Today import java.util.Collections; List<String> list = …; Collections.sort(list); www.javapolis.com

  14. Extension methods • Proposed import static java.util.Collections.sort; List<String> list = …; list.sort(); www.javapolis.com

  15. 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(); www.javapolis.com

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

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

  18. 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); } www.javapolis.com

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

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

  21. 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 } www.javapolis.com

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

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

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

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

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

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

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

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

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

  31. Thank you for your attention

More Related