Java 10 Features

 

 Java 10 Features:



  1. Core library changes:

  1.  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.
  2. 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.
  3. c.System Property to Disable JRE Last Usage Tracking 
  4. Local variable ‘var’ type inference

  1. Parallel Full GC for G1 
  2. Improve docker container detection and resource configuration usage
  3. Allow more flexibility in selecting Heap % of available RAM , Heap Allocation on Alternative Memory Devices
  4. Thread-Local Handshakes


Removals:


  1.  Removal of Support for Using Old LookAndFeel 
  2.  Removal of Runtime.getLocalizedInputStream and getLocalizedOutputStream Methods 
  3. Removal of RMI Server-Side Multiplex Protocol Support
  4.  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?



  1. var var = 5; // syntactically correct

// var is the name of the variable


  1. public static void var() { // syntactically correct 

}


  1. package var; // syntactically correct


  1. 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


  1. interface var{ } // Compile Error
  2. 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

  1. 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

  1. 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

  1. not allowed for array declaration

       var[] nums = {1,2,3,4,5}; 

       var actorArr[] = new Integer[10];

  1. not allowed for method reference

var minimum = Math::min;

  1. generics


var map1 = new HashMap(); // Inferred as HashMap

var map2 = new HashMap<>(); // Inferred as HashMap<Object, Object>


  1. Allowed for anonymous inner classes
  2. Not allowed as catch formal
  3. Not allowed in method return type
  4. Not allowed in method parameter
  5. 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

Popular posts from this blog

Java 12 Features

Java 13 features

Java 11 Features