Java 9 ships with some minor—yet awesome—new language features that make developing easier and cleaner. In this post, we’ll take a look at three of these new features.

Private interface methods

You probably remember interface methods, which were introduced in Java 8. These are required so that Java itself, as well as framework vendors, can add new methods to interfaces without breaking pre-existing implementations. As these methods can become rather complex, Java now enables you to declare interface methods as private. This allows for extracting common code from interface methods while hiding the code from external use.

public interface DeepThought {
	abstract int foo();
	abstract int bar();

	default int fooDecorated() {
		return decorate(foo());
	}

	default int barDecorated() {
		return decorate(bar());
	}

	private int decorate(int value) {
		return Math.min(42, Math.max(42, value));
	}
}

“Effectively final” variables in try-with-resources

You probably also remember the concept of “effectively final” variables, which were also introduced in Java 8. Developers don’t need to declare local variables as final anymore (for example, to use them in anonymous inner classes) when the variables are only initialized and never modified again. This concept has been extended to the try-with-resources block. Assuming a closeable resource (for example, a Stream), you previously had to introduce an artificial variable to use the resource in a try-with-resources block, such as the following:

void read(MyInputStream in) {
    try(MyInputStream i = in) {
        in.readAllTheThings();
    }
}

This version makes sense if in is a complex expression (for example, a new instruction), but not if it is just a local variable. To make such cases easier on the eyes, the expression in the try block now allows any L-value (i.e., any expression that you can find on the left of an assignment operator). We can then simplify the example to this:

void read(MyInputStream in) {
    try(in) {
        in.readAllTheThings();
    }
}

Variable handles

We discussed the introduction of the module system in a previous blog post. Oracle has put a lot of effort into providing functionality that was previously only available by accessing JDK-internal classes. For example, to implement a low-level compare-and-swap, a pretty common implementation was to dig out the sun.misc.Unsafe class via reflection and to manipulate Java fields directly:

public class Counter {
	public static volatile int value;
}

Field unsafeField = Unsafe.class.getDeclaredField(“unsafe”);
unsafeField.setAccessible(true); //lets hope there is no security m.
Unsafe unsafe = (Unsafe) unsafeField.get(null);
Field field = Counter.class.getDeclaredField(“value”);
long offset = Unsafe.fieldOffset(field);
for(;;) {
	int expected = Counter.value;
	int next = expected + 1;
	if(unsafe.compareAndSwapInt(Counter.class, offset, value, value + 1))
		break;
}

The Unsafe class is only for internal use by the JDK and the Just-in-time compiler. It’s not part of the Java Specification and its use is discouraged. However, as this class is the only way to implement specific low-level operations, developers have used it a lot. Now, with the new module system in place, the VM no longer allows access to the Unsafe class, not even via reflection. To solve this problem, Java 9 now provides an official, supported, and much simpler way to implement this kind of low-level functionality via the VarHandle class:

VarHandle handle = MethodHandles.lookup().findVarHandle(Counter.class, “value”, int.class);
for(;;) {
	int expected = value;
	int next = value + 1;
	if(handle.compareAndSet(Counter.class, expected, next)) break;
}