Johannes Thönes

Software-Developer, ThoughtWorker, Permanent Journeyman, Ruby Enthusiast, Java and Devils Advocate.

Java 8 Status Updates

The two big new language features of the upcoming Java SE 8 release are Lambda Expressions and Modularity. For both, status updates have been released these days. I’ll share the links with you, so you might read through them over the holidays ;-)

The Java SE 8 release is planned for mid 2013 by Oracle.

Project Lambda

Project Lambda as well as the JSR-335 wants to provide means for modelling of code as data in Java - in non-exact, colloquial words one could say it aims for functions as first-class objects in Java. To do so, Project lambda wants to provide the following four extensions to the Java language:

  1. Lambda Expressions or Closures which allow the programmer to specify a piece of executable code  in an idomatic way. They can be stored in a variable, passed to a method as argument or used as return value of a method.

  2. Expandend Target Typing to bind the Lambda Expressions to objects of a specific type (type inference). These types can be so-called Function Interfaces - Java interfaces with exactly one method.

  3. Method and Constructor References to allow the programmer to use existing methods on objects to be bound to a a Function Interface.

  4. Default or Virtual Extension Methods to add more methods to existing interfaces without breaking existing implementations  (especially in the collection library).

To give you an idea, here is a piece of code using anonymous inner-classes for some collection logic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
List students = // ...
students.filter(new FilerFunction(){
    @Override
    public boolean filter(Student s){
      return s.getEntryYear() == 2011;
    }
    })
  .map(new MapFunction<Student,Integer>(){
    @Override
    public Integer map(Student s){
      return s.getGrade();
    }
  })
  .reduce(new ReduceFunction<Integer>(){
    @Override
    public Integer reduce(Integer value1, Integer value2){
      Math.max(value1, value2);
    }
  });

In contrast the following code using the features on their way with Project Lambda:

1
2
3
4
5
List students = // ...
students.paralell()
  .filter(s -> s.getEntryYear() == 2011)
  .map(s -> s.getGrade())
  .reduce(Math::max);

The information about the current state from Specification Lead and OpenJDK Project Lead Brian Goetz can be found at State of the Lambda.

Project Jigsaw - Modularity for the Java Platform

In Project Jigsaw, the OpenJDK community lead by Oracle tries to introduce modularity into Java the language. The approach will be different from e.g. OSGi, because they want to establish it on the language level - with static compile time checking. The Oracle people always say they strive for compatibility of Jigsaw with OSGi.

Marc Reinhold, Oracles Chief Platform Architect and OpenJDK Project Lead, describes three principles of the modularity approach:

  • Modularity is a language construct - The best way to support modular programming in a standard way in the Java platform is to extend the language itself to support modules. Developers already think about standard kinds of program components such as classes and interfaces in terms of the language; modules should be just another kind of program component.

  • Module boundaries should be strongly enforced -  A class that is private to a module should be private in exactly the same way that a private field is private to a class. In other words, module boundaries should determine not just the visibility of classes and interfaces but also their accessibility. Without this guarantee it is impossible to construct modular systems capable of running untrusted code securely.

  • Static, single-version module resolution is usually sufficient - Most applications do not need to add or remove modules dynamically at run time, nor do they need to use multiple versions of the same module simultaneously. The module system should be optimized for common scenarios but also support narrowly-scoped forms of dynamic multi-version resolution motivated by actual use cases such as, e.g., application servers, IDEs, and test harnesses.

For the programmer using Jigsaw, it will be especially noticable because the language will now have three phases (instead of two):

  • Compile Time: The classes of a module are compiled. The compiled classes together with the resources (configuration files, metadata files etc.) are packed together in an archive in the format of JMOD (for java module):

  • Install Time: On any computer having the JRE installed, there will be a module library. Here the user can install java modules.

  • Run Time: A module defining a main class (Invokable Module) can be executed. The JVM will load this module and any module it requires from the module library and then execute the code.

Information about the the current state of Project Jigsaw from Marc Reinhold can be found at Project Jigsaw: The Big Picture — DRAFT 1.