language features for jdk7 neal gafter joshua bloch google
TRANSCRIPT
![Page 1: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/1.jpg)
![Page 2: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/2.jpg)
Language Features for JDK7
Neal GafterJoshua Bloch
![Page 3: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/3.jpg)
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
![Page 4: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/4.jpg)
www.javapolis.com
Improved type inference: constructors
• Today:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
![Page 5: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/5.jpg)
www.javapolis.com
Improved type inference: constructors
• Proposed:
Map<String, List<String>> anagrams = new HashMap<>();
![Page 6: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/6.jpg)
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());
![Page 7: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/7.jpg)
www.javapolis.com
Improved type inference: argument positions
• Today:
public <E> Set<E> emptySet() { … }
void timeWaitsFor(Set<Man> people) { … }
// OK
timeWaitsFor(Collections.<Man>emptySet());
![Page 8: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/8.jpg)
www.javapolis.com
Improved type inference: argument positions
• Proposed:
public <E> Set<E> emptySet() { … }
void timeWaitsFor(Set<Man> people) { … }
// OK
timeWaitsFor(Collections.emptySet());
![Page 9: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/9.jpg)
www.javapolis.com
Enum comparison
• Today
enum Size { SMALL, MEDIUM, LARGE }
if (mySize.compareTo(yourSize) >= 0)
System.out.println(“You can wear my pants.”);
![Page 10: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/10.jpg)
www.javapolis.com
Enum comparison
• Proposed
enum Size { SMALL, MEDIUM, LARGE }
if (mySize > yourSize)
System.out.println(“You can wear my pants.”);
![Page 11: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/11.jpg)
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);
}
}
![Page 12: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/12.jpg)
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);
}
}
![Page 13: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/13.jpg)
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
![Page 14: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/14.jpg)
www.javapolis.com
Extension methods
• Today
import java.util.Collections;
List<String> list = …;
Collections.sort(list);
![Page 15: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/15.jpg)
www.javapolis.com
Extension methods
• Proposed
import static java.util.Collections.sort;
List<String> list = …;
list.sort();
![Page 16: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/16.jpg)
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();
![Page 17: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/17.jpg)
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();
![Page 18: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/18.jpg)
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
![Page 19: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/19.jpg)
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);
}
![Page 20: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/20.jpg)
www.javapolis.com
Improved catch clauses: catching multiple types
• Proposed:
try {
return klass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e);
}
![Page 21: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/21.jpg)
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
}
![Page 22: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/22.jpg)
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
}
![Page 23: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/23.jpg)
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);
}
![Page 24: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/24.jpg)
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;
}
![Page 25: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/25.jpg)
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;
}
![Page 26: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/26.jpg)
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;
}
![Page 27: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/27.jpg)
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) { … }
}
![Page 28: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/28.jpg)
www.javapolis.com
Self Type
• Today
ByteBuffer buf = ...;
buf.flip().position(12); // OK
buf.flip().put(12); // Error
![Page 29: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/29.jpg)
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) { … }
}
![Page 30: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/30.jpg)
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 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 };
![Page 31: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/31.jpg)
Q&AView JavaPolis talks @ www.parleys.com
![Page 32: Language Features for JDK7 Neal Gafter Joshua Bloch Google](https://reader033.vdocuments.site/reader033/viewer/2022051819/55146dd6550346284e8b5ec4/html5/thumbnails/32.jpg)
Thank you for your attention