Java 10 Features
Java 10 Features:
- Core library changes:
- Optional.orElseThrow: A new method orElseThrow has been added to the Optional class. It is synonymous with and is now the preferred alternative to the existing get method.
- APIs for Creating Unmodifiable Collections : The List.copyOf, Set.copyOf, and Map.copyOf methods create new collection instances from existing instances. New methods toUnmodifiableList, toUnmodifiableSet, and toUnmodifiableMap have been added to the Collectors class in the Stream package. These allow the elements of a Stream to be collected into an unmodifiable collection.
- c.System Property to Disable JRE Last Usage Tracking
- Local variable ‘var’ type inference
- Parallel Full GC for G1
- Improve docker container detection and resource configuration usage
- Allow more flexibility in selecting Heap % of available RAM , Heap Allocation on Alternative Memory Devices
- Thread-Local Handshakes
Removals:
- Removal of Support for Using Old LookAndFeel
- Removal of Runtime.getLocalizedInputStream and getLocalizedOutputStream Methods
- Removal of RMI Server-Side Multiplex Protocol Support
- Removal of Common DOM APIs
Unmodifiable Collections with copyOf:
var al = new ArrayList<Integer>();
al.add(10);
al.add(30);
List<Integer> copyList = List.copyOf(al);
al.add(45); // this will not be reflected in the copyList as its snapshot is already taken above.
//copyList.add(50); it throws UnSupportedOperationException if we try to add to immutable collection
System.out.println(copyList);
Collectors.toUnmodifiableList(), Collectors.toUnmodifiableSet(), Collectors.toUnmodifiableMap():
List<Integer> final_al = al.stream().map(x->x+5).collect(Collectors.toUnmodifiableList());
// final_al.add(45); it throws UnSupportedOperationException if we try to add to immutable collection
System.out.println(final_al);
What is type Inference?
Type inference is Java compiler’s ability to look at each method invocation and corresponding declaration to determine the type argument (or arguments) that make the invocation applicable
How does Local Variable Type Inference work?
Parsing a var statement, the compiler looks at the right-hand side of the declaration, aka initializer, and it infers the type from the right-hand side (RHS) expression.
var is not a keyword, It’s a reserved type name. What does it mean?
- var var = 5; // syntactically correct
// var is the name of the variable
- public static void var() { // syntactically correct
}
- package var; // syntactically correct
- class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
^
as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error
- interface var{ } // Compile Error
- Cannot use ‘var’ on variables without initializer
var x;
LocalTypeInference.java:37: error: cannot infer type for local variable x
var x;
^
(cannot use 'var' on variable without initializer)
1 error
- Cannot be used for multiple variable definition
var x = 5, y = 10; //compile error
LocalTypeInference.java:41: error: 'var' is not allowed in a compound declaration
var x = 5, y = 10;
^
1 error
- Null cannot be used as an initializer for var
var author = null; // Null cannot be inferred to a type
LocalTypeInference.java:47: error: cannot infer type for local variable author
var author = null;
^
(variable initializer is 'null')
1 error
- not allowed for array declaration
var[] nums = {1,2,3,4,5};
var actorArr[] = new Integer[10];
- not allowed for method reference
var minimum = Math::min;
- generics
var map1 = new HashMap(); // Inferred as HashMap
var map2 = new HashMap<>(); // Inferred as HashMap<Object, Object>
- Allowed for anonymous inner classes
- Not allowed as catch formal
- Not allowed in method return type
- Not allowed in method parameter
- Final can be used with var
Benefits of Local Variable Type Inference
- It improves the developer experience
- It reduces code ceremony
- It reduces boiler plate code
- Increases code clarity
Reference: https://www.journaldev.com/19871/java-10-local-variable-type-inference
https://howtodoinjava.com/java10/var-local-variable-type-inference/
https://openjdk.java.net/projects/amber/LVTIFAQ.html
Comments
Post a Comment