Top Banner
32

Language Features for JDK7 Neal Gafter Joshua Bloch Google.

Mar 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Language Features for JDK7 Neal Gafter Joshua Bloch Google.
Page 2: Language Features for JDK7 Neal Gafter Joshua Bloch Google.

Language Features for JDK7

Neal GafterJoshua Bloch

Google

Page 3: Language Features for JDK7 Neal Gafter Joshua Bloch Google.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Q&AView JavaPolis talks @ www.parleys.com

Page 32: Language Features for JDK7 Neal Gafter Joshua Bloch Google.

Thank you for your attention