Skip to content

Java Language Change

  • by

I’ve been to JavaEdge 2008 last week, and while many of the sessions were interesting, I found the one about the Java Language Change particularly intriguing.

During the session 10 proposed changes were introduced, all of them minor by definition (as stated by Sun in the JavaOne 2008 TS-5881 presentation), and everyone got to (anonymously) vote.

Here is the list of proposed changes, and even if you don’t get to vote, I think it’s interesting and thought provoking:

1. Map-for-each:


for (String key, Integer value : map) {
...
}

2. For-each iteration control:

for (String str : list : it) {
if (str.length() > 100) {
it.remove();
}
it.set(...);
it.index();
it.isFirst();
it.isLast();

}

3. List/map access:


List<String> list = ...
Map<String, Job> map = ...
Map<String, Map<String, Task>> combined = ...

String str = list[0];
list[0] = "Hi";
Integer value = map[str];
map["Hi"] = 56;
Task task = combined["Test"]["Monitor"];

4. Infer generics in declarations:

Instead of
List<String> list = new ArrayList<String>();
use
List<String> list = new ArrayList<>();

5. Multi-catch of Exceptions:


try {
doSomething();
doSomethingElse();
}
catch (IOException, SQLException ex) {
// handle..
}

6. String switch:



String str = ..
switch (str) {
case "name":
processName();
break;
case "surname":
processSurname;
...
}

7. String interpolation:


String name = ...
String Value = ..
String out = $"The value of ${name} is ${value}";

8. Multi-line Strings


String sql =
"""SELECT firstname, lastname, birth_date,
telephone, account
FROM
preson, phone, email
WHERE
..
""";

9. Resource management:



try (FileReader in = new FileReader(file)) {
processFile(in);
} // and resource validation and release is done automatically

10. Null-handling:



Session sess = ..

String code = sess?.preson()?.address()?.postcode(); // if anything preceding ? is null, the result is null

What’s your vote? 😉

Leave a Reply

Your email address will not be published. Required fields are marked *