language features for jdk7
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Language Features for JDK7
Neal GafterJoshua Bloch
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
www.javapolis.com
Improved type inference: constructors
• Today:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
www.javapolis.com
Improved type inference: constructors
• Proposed:
Map<String, List<String>> anagrams = new HashMap<>();
www.javapolis.com
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
Improved type inference: argument positions
• Today:
public <E> Set<E> emptySet() { … }void timeWaitsFor(Set<Man> people) { … }
// OKtimeWaitsFor(Collections.<Man>emptySet());
www.javapolis.com
Improved type inference: argument positions
• Proposed:
public <E> Set<E> emptySet() { … }void timeWaitsFor(Set<Man> people) { … }
// OKtimeWaitsFor(Collections.emptySet());
www.javapolis.com
Enum comparison
• Today
enum Size { SMALL, MEDIUM, LARGE }
if (mySize.compareTo(yourSize) >= 0) System.out.println(“You can wear my pants.”);
www.javapolis.com
Enum comparison
• Proposed
enum Size { SMALL, MEDIUM, LARGE }
if (mySize > yourSize) System.out.println(“You can wear my pants.”);
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); }}
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); }}
www.javapolis.com
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
Extension methods
• Today
import java.util.Collections;
List<String> list = …;Collections.sort(list);
www.javapolis.com
Extension methods
• Proposed
import static java.util.Collections.sort;
List<String> list = …;list.sort();
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();
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();
www.javapolis.com
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
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
Improved catch clauses: catching multiple types
• Proposed:
try { return klass.newInstance();} catch (InstantiationException | IllegalAccessException e) { throw new AssertionError(e);}
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}
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}
www.javapolis.com
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
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
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
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
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
Self Type
• Today
ByteBuffer buf = ...;buf.flip().position(12); // OKbuf.flip().put(12); // Error
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) { … }}
www.javapolis.com
Typedef
import java.util.Map<String,String> as MyProperties;import java.util.Map<String,T> as Lookup<T>;// if we add BGGA function typesimport { double => double } as DoubleFcn;
Alternatively
static class MyProperties = Map<String,String>;static class Lookup<T> = Map<String,T>;// if we add BGGA function typesstatic class DoubleFcn = { double => double };
Q&AView JavaPolis talks @ www.parleys.com
Thank you for your attention