Créer un site internet

Project Amber

Project Amber relates to several small features which are being added to the JDK over time. The aim of the project is to improve productivity for Java developers by making code cleaner with less boilerplate.

1. Local-Variable Type Inference (var) (JDK 10):

Developers frequently complain about the degree of boilerplate coding required in Java. In JAVA 7, diamond operator has been introduced to make it easier to work with generics. There is no need to rewrite the same type in the same statement.

List<String> strings = new ArrayList<String>(); // Java 6 
List<String> strings = new ArrayList<>(); // Java 7

Enhancements in Java 8 along with lambda and stream support:

Predicate isEmpty= (String s) -> s.length==0;
Predicate isEmpty= s -> s.length==0;

In JAVA 10, local-Variable Type Inference are introduced. Type declarations are often unnecessary because the important information in the variable is the name and not the type. we can use the var keyword instead of the full type definition, and the compiler will automatically work out the correct type information to use. With this enhancement, a statement like:

var list = new ArrayList();  // infers ArrayList
var stream = list.stream();          // infers Stream

The compiler will be able to infer declaration types of local variables except in some cases:

  • Local variables without initializers, such as File inputFile;
  • Local variables initialized to null.
  • Initializers that expect a target type, such as a lambda, a method reference or an array initializer.
var unknownType; // No value provided to infer type from
var nullType = null; // Explicit value provided but it's null
var lambdaType = () -> System.out.println("Lambda"); // Lambda without defining the interface
		
// Not allowed, lambda expression needs an explicit target-type
var func = s -> Integer.valueOf(s);
 
// Not allowed, method reference needs an explicit target-type
var biConsumer = LogProcessor::process;
		 
// Not allowed, array initializer needs an explicit target-type
var array = { 1, 2, "3" };

2.Raw String Literals: JAVA 12 preview features [--enable-preview]

JAVA needs a mechanism for capturing literal strings as-is, without special handling of Unicode escaping, backslash, or new lines. Raw String Literals are enclosed in backtick marks instead of double quotes and can contain any characters at all inside of them.

3. Switch Expressions (Preview) JAVA 12 [Groovy]

The motivation of this feature is to simplify the code for the switch expression. The actual switch expression syntax is error prone (Forget a break).

There is no default needed since enum classes are final. The compiler ca tell that you have covered all the cases. The new design of switch expression is similar to C and C#.

4. Data Class: record [Kotlin or SCALA]

In JAVA, to write a simple data class we should write a repetitive code : constructors, accessors, equals(), hashCode(), toString(), etc. And developers are tempted to delete equals(), hashCode and toString code leading to surprising behavior or poor debuggability.We can often generate this code using IDE but it helps us in writing code but not in reading. And repetitive code is a good place for bugs to hide.

Both of these can now be eliminated by using record to define classes by their state.

The record is immutable.In the record class, we can:

  • override hashcode,
  • add a method.
  • customize the constructors
  • but we can not add instance field

5. Pattern Matching for instanceof (Preview):

JAVA introduced an enhancement to make it easier to work with instanceof operator using pattern matching. In current JAVA, we check if an expression has of a particular type then we cast the value to this type. It is tidious and error-prone code to check and test.

With pattern matching, if obj is an instance of String, then it is cast to String and assigned to the binding variable s. In this case, wehave only one statement without duplication and no error risks.

References:

Graham, Cox.Introduction to Project Amber. https://www.baeldung.com/java-project-amber

Brian, Goetz. Data Classes and Sealed Types for Java. February 2019, https://cr.openjdk.java.net/~briangoetz/amber/datum.html

Mark Reinhold. Java, Today and Tomorrow. Devoxx 2018, Belgium, https://www.youtube.com/watch?v=Csc2JRs6470

Brian, Goetz. Java Futures. Devoxx 2018, Belgium, https://www.youtube.com/watch?v=4r2Wg-TY7gU

Brian, Goetz. Java Language Futures - All Aboard Project Amber. Devoxx 2017, Belgium, https://www.youtube.com/watch?v=qul2B8iPC-o&feature=youtu.be

https://openjdk.java.net

Add a comment