This document discusses 5 missing features in Java and how they are addressed in a new version of Java:
1. Collection literals allow for more concise initialization of collections using syntax like newArrayList("foo", "bar", "baz").
2. Object literals enable more readable construction of objects using anonymous classes and initializers rather than imperative style.
3. Closures support lambda expressions to concisely represent operations on collections.
4. Properties allow direct access to fields while supporting interception through setters.
5. Multiline strings address the lack of multiline string literals in Java through source code transformations.
7. Object Literals
Java enforces imperative style.
Person p = new Person();
p.setFirstName("Hans");
p.setLastName("Albers");
Address address = new Address();
address.setStreet("Am Germaniahafen 1");
address.setCity("Kiel");
p.setAddress(address);
8. Object Literals
Person p = new Person() {{
firstName = "Hans";
lastName = "Albers";
address = new Address() {{
street = "Am Germaniahafen 1";
city = "Kiel";
}};
}};
9. Object Literals
Person p = new Person() {{
firstName = "Hans";
lastName = "Albers";
address = new Address() {{
street = "Am Germaniahafen 1";
city = "Kiel";
}};
}};
anonymous classes
10. Object Literals
Person p = new Person() {{
firstName = "Hans";
lastName = "Albers";
address = new Address() {{
street = "Am Germaniahafen 1";
city = "Kiel";
}};
}};
Initializer
11. No Closures
My friends’ names in a comma
separated string.
List<Person> myFriends = ...
StringBuilder sb = new StringBuilder();
for (Person person : myFriends) {
sb.append(person.getName())
.append(“, “);
}
String result =
sb.toString().substring(0, sb.length()-2);
12. How about this?
List<Person> myFriends = ...
String names = joinFrom(myFriends).getName();
generics and proxies!
LambdaJ - http://code.google.com/p/lambdaj/
13. Closures in Java 8
List<Person> myFriends = ...
List<String> names = myFriends
.map(#{ p -> p.getName(); });
15. Properties
class Person {
public String firstName;
public void setFirstName(String s) {
firstName = process(s);
}
}
Person p = ....
p.firstName = “Horst”;
Byte code manipulation during class loading!
16. Multiline Strings
public static void main(String[] args)
{
System.out.println($(/*
Wow, we finally have
multiline strings in
Java! HOOO!
*/));
}
Source code & stack trace information