JDK 8 New Features



1) New package (java.time) introduced

2) Arrays.parallelSort(fruits1); instead of Arrays.sort(fruits);
               bit fast the new one.

3) @FunctionalInterface newly introduces, it will recognize at compile time if interface have more than one abstract method.

@Functional
Interface public interface Demo {
               public abstract void display();
               public abstract void show();
}

               It will give compile error because it contains two abstract methods.

4) A known bug with the way String.hashCode() for that given better solution.
               You can see that known bug with below stack overflow link.

               the HashMap bucket uses a linked list to store the map entries. Using this algorithm, if there are a high number of collisions, then the complexity of the hash changes from O(1) to O(N). To resolve this, once the number of items in a bucket reaches a certain threshold, the bucket will switch to using a balanced tree algorithm in order to reduce the complexity to O(log n).

5) forEach(we can see this with below "lambda" feature in second example)

6) Lambda Introduction("-> " is known as lambda expression.)
               When a Lambda expression is written, it is translated into a functional interface at compile time. Here is an example of using Lambda expressions to replace an anonymous inner class with much cleaner and more readable code.
Example 1:
Old way without Lambda:

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(“Action Detected”);
}}
);
New way with Lambda:
button.addActionListener(e -> {
System.out.println(“Action Detected”);
} );
Example 2:
List<String> alphabets = Arrays.asList("A", "B", "C", "D");
Old:
for(String str : alphabets) {
System.out.print(str + "\t");
}

New:

alphabets.forEach(str -> { System.out.print(str + "\t"); } );

Different ways:

System.out.println("\n\nTo print using Lamda in Java 8 in more readable way using type:");
cities.forEach((String str) -> System.out.print(str + "\t"));
System.out.println("\n\nTo print using Lamda in Java 8 without using type but using \"value\":");
cities.forEach(value -> System.out.print(value + "\t"));
System.out.println("\n\nTo print using more concisely with :: operator:");
cities.forEach(System.out :: println);

Example 3:
Sort By age
 //sort by age
 Collections.sort(listDevs, new Comparator() {
  @Override
  public int compare(Developer o1, Developer o2) {
   return o1.getAge() - o2.getAge();
  }
 });

 //lambda
 listDevs.sort((Developer o1, Developer o2)->o1.getAge()-o2.getAge());
 //lambda, valid, parameter type is optional
 listDevs.sort((o1, o2)->o1.getAge()-o2.getAge());

7) Replacement of PremGen with Metaspace
JDK 1.8 officially announced the removal of Permanent Generation (PremGen) space and in its place introduced Metaspace. In HotSpot VM, the PremGen used to give OutOfMemoryError due to depletion of space, may sometimes be caused by memory leaks while loading and unloading a J2EE applications.
In practice, the IBM JRE and Oracle JRockit are not using PremGen space and are using the native memory (C-Heap) to store a class’s metadata. This concept is taken into JDK 1.8 for Java VM.
From JDK 1.8, most of the memory allocation for storing metada is done through native memory. The existing classes used to retrieve the metadata of a class no more works with metaspace. By default, the metaspace allocation memory depends on the native memory availability, OS virtual memory availability and on JVM of 32-bit or 64-bit. A flag introduced to limit the maximum memory allocation for metaspace – MaxMetaspaceSize. If this flag is not set, the metaspace will dynamically updated, at intervals, as per the requirement of the application running.
The garbage collector is triggered to go for garbage collection when the metadata usage is more than the size of MaxMetaspaceSize.
Due to removal of PremGen space, we cannot configure the space through XX:PermSize & -XX:MaxPermSize

0/Post a Comment/Comments

Previous Post Next Post