Pure Danger Tech


navigation
home

JavaOne: Project Coin

02 Jun 2009

Joe Darcy has been running Project Coin to collect, sift through, and implement small languages changes for Java. This talk walks through some of the proposals that have been selected for consideration. At this point there are a dozen or so in play but only about half of those will actually be added for Java 7. Below are some examples…

Better integer literals – the idea here is to fix a handful of deficiencies in integer literals, specifically:

  • binary literals – int mask = 0b1010;
  • underscores for readability – long big = 9_223_213_123;
  • unsigned literals – byte b = 0xFFu

Null handling

Null handling adds the new “Elvis” operator (named for the emoticon profile of the “?”) that allows you to pack a null check into an operator. Groovy has an operator like this and it’s useful to avoid having nested if … == null checks while walking through a chain of methods.

An example:

Integer i = returnIntOrNull() ?: ultimateAnswer();
which will invoke the first method and short-circuit to return null if the first method returns null. Otherwise it will go on to invoke the second method. These can be chained together of course and null will be returned if any method in the chain returns null.

UPDATE: The description above is really a description of the ?. null-safe member selection operator, not the binary Elvis operator (which is a special case version of the ? : construct in Java with a null check as the condition). The correct description should be:

…which will invoke the first method and return it if non-null. If null, the second argument is returned.

Simplified varargs method invocation

This is a rough edge that Bob Lee has been trying to fix. Some example code:

static<T> List<T> asList(T... elements)

static List<Callable<String>> stringFactories() {
	Callable<String> a,b,c;
	...	
	return asList(a,b,c);		// Warning here at call site but nothing we can do
}

The varargs (implemented as an Object[]) and generics don’t play well together. I think the suggested fix is to make the warning at the declaration site instead of the call site to cut down on annoying warnings.

Strings in switch

Pretty simple change to allow string literals to be compared in a switch as well as other existing literal types:

switch(s) {
	case "foo": return 1;
	case "bar": return 2;
}

Exception handling improvements

I would have to go back and check the spec to see what the current proposal is but he mentioned using the disjunctive operator to catch and handle multiple exception types in the same catch block.

Indexing syntax for lists and maps

This addition would allow lists and maps to use the [] operator to create and retrieve elements from maps and lists:

It looks like the solution direction is to define the methods necessary for this support in an interface so that user code can also implement those interfaces and get [] support.

Collection literals

Many (most?) languages now have the ability to create collections with a literal syntax. This is much more compact than the current code which requires constructing a collection and adding elements to it, one per line.

List<String> list = ["a", "b"];
String firstElement = list[0];

Large arrays

Currently, arrays are indexed by an int. In our ever-larger JVMs, some people are starting to hit that int limit and need long-indexed arrays. There has been a proposal to modify the language to support this but that’s a fairly involved and invasive change. A somewhat simpler change is to add support in the JDK with a library-based change and that is currently under investigation. In the future, if there is JVM-based support for this, the library version can take advantage of it.

JSR 292 / invokedynamic

JSR 292 is adding new support for invokedynamic and they are working on providing an API to surface that JVM feature into the Java language as a dynamic API that would make implementing JVM languages in Java much easier than going through a library like ASM.

Diamond type inference

Currently, if you construct a collection with generics, you must repeat the generics on both the LHS and RHS of the assignment statement. This change would allow you to shorten the RHS to <>.

At the end Joe showed a small demo of NetBeans giving some early help for strings in switch and the diamond type inference operator. Joe also showed off some improved javac error messages, in particular around invalid generics, which currently have some fairly esoteric forms.

If you’re interested in seeing how these changes might impact your own program, there is a java checker program that can analyze your code base and produce statistics about how many instances (and percentages) might be changed. You can find some statistics for recent proposals on the mailing list.