Wednesday, 10 July 2019

Difference between Java 7, 8 and Java 10

Java versions explained:

Java 7
  • Diamond Operator
  • String in switch
  • Automatic Resource Management
  • Numeric literals with underscores
  • Improved exception handling
  • New file system API [NIO 2.0]
  • Fork and Join framework

Diamond Operator:

Before Java 7:

Map<String, List<Trade>> trades = new TreeMap<String, List<Trade>> ();

From Java 7:
Map<String, List<Trade>> trades = new TreeMap<> ();

Improved Exception handling:

Before Java 7:
public voidoldMultiCatch() {

            try{

                  methodThatThrowsThreeExceptions();

            } catch(ExceptionOne e) {

                  // log and deal with ExceptionOne

            } catch(ExceptionTwo e) {

                  // log and deal with ExceptionTwo

            } catch(ExceptionThree e) {

                  // log and deal with ExceptionThree

            }

      }

From Java 7:
public voidnewMultiCatch() {

            try{

                  methodThatThrowsThreeExceptions();

            } catch(ExceptionOne | ExceptionTwo | ExceptionThree e) {

                  // log and deal with all Exceptions

            }

      }

However, if you have bunch of exceptions that belong to different types, then you could use “multi multi-catch” blocks too. 
public void newMultiMultiCatch() {

            try{

                  methodThatThrowsThreeExceptions();

            } catch(ExceptionOne e) {

                  // log and deal with ExceptionOne



            } catch(ExceptionTwo | ExceptionThree e) {

                  // log and deal with ExceptionTwo and ExceptionThree

            }



      }


Java 8
  • Functional interfaces
  • Lambdas
  • New Date and Time API
  • Concurrency
    • Classes and interfaces have been added to java.util.concurrent package
    • Methods have been added to java.util.concurrent.ConcurrentHashMap class to support aggregate operations.
    • The java.util.concurrent.locks.StampedLock class has been added to provide a capability based lock with three modes for controlling read/write access.
  • Collections
    • Classes in the new java.util.stream package provide a Stream API to support functional-style operations on streams of elements. The Stream API is integrated into Collections API, which enables bulk operations on collections, such as sequential or parallel map-reduce transformations.
    • Performance improvement for HashMap  with key collisions.


Java 10


New features:

  • Local variable Type
  • New Byte Code generation for Enhanced For loops
  • Parallel Full Garbage Collection support for G1
  • Removal of javah tool
  • Heap allocation on alternative memory devices

Local variable Type Inference:

Java 10 introduced var as a reserved type name to reduce verbosity[extra or empty words].
The advantage is that we no need to explicitly declare the variable type, so it reduces boiler plate code.

e.g.:

var list = new ArrayList<>();
Printing it , shows: []

var isValid = true;
Printing it, shows: true

If(isValid){
            System.out.println(“valid”);
}
Prints : valid
var i = 10;
Prints : 10

Note: We cannot use var as class or interface name.

More example:
class SampleDemo{
            public Map<String, String> getMapData(){
                        return new HashMap();
}          
}

var sampleDemo = new SampleDemo();
var map = sampleDemo.getMapData();
Printing map shows: {}

Disadvantage with var type is that we need to find the datatype it is implying.


Restrictions:

var i;  // invalid
var i, j = 0;  // invalid
var i = null;  // invalid


Parallel full GC for G1:
Java 9 made G1 as the default, and in java 10, parallel full garbage collection support for G1 will improve the worst-case latencies. It parallelized the mark-sweep-compact algorithm.


Removal of javah:
The native header tool is now removed and we can generate the native header using the java compiler with the –h option.

Heap allocation on alternative memory devices:
Enable the HotSpot VM to allocate the Java object heap on an alternative memory device, such as an NV-DIMM, specified by the user.

Use case:
In multi-JVM deployments some JVMs such as daemons, services, etc., have lower priority than others.
NV-DIMMs would potentially have higher access latency compared to DRAM.
Low-priority processes can use NV-DIMM memory for the heap, allowing high-priority processes to use more DRAM.


If this post is really helpful, Kindly leave a comment.

Thanks!

No comments:

Post a Comment