Tuesday, 27 May 2014

JAVA SE 8 Features

JAVA 8 Features List:

  1. Virtual  Extension Methods: Interface Default Methods 
  2. Virtual  Extension Methods: Interface Static Methods
  3. "Class Wins" Rule
  4. Functional Interface, @FunctionalInterface
  5. Enhanced Type Inference
  6. Lambda Expressions
  7. Functional Interfaces - Function,Consumer,Supplier,Predicate etc
  8. Method References
  9. Constructor References
  10. Stream API
  11. Stream Primitive Specilizations
  12. Date & Time API
  13. Collectors
  14. StringJoiner
  15. Collections API Enhancements
  16. Concurrency Utils Enhancements
  17. java.util.concurrent.atomic Enhancements
  18. Collections Bulk Data Operations
  19. StampedLock
  20. Fork/Join Framework Enhancements
  21. Internal Iteration
  22. Parallel Array Operations
  23. Parallel Collection Operations
  24. Parallel Stream Operations
  25. Arrays Spliterators
  26. Collections Spliterators
  27. Streams Spliterators
  28. Optional
  29. Type Annotations
  30. Method Parameter Reflection
  31. Repeating Annotations
  32. Base64 Encoding and Decoding
  33. IO and NIO2 Enhancements 
  34. Nashorn JavaScript Engine
  35. javac Enhancements
  36. JVM Changes
  37. Java 8 Compact Profiles: compact1,compact2,compact3
  38. JDBC 4.2
  39. JAXP 1.6
  40. Java DB 10.10
  41. Networking
  42. Java Security Changes
Now we'll discuss each topic in detail.

Optional:

  • Optional is a final Class in java.util package
  • It is an alternative to "null" value
  • It is used to avoid "NullPointerException"
Example-1:

package com.way2it.java8.optional;
import java.util.Optional;

public class OptionalTest {
public static void main(String[] args) {
    Optional<String> optional = Optional.of("Rams");
        System.out.println("optional.isPresent() : " + optional.isPresent()); 
        System.out.println("optional.get() : " + optional.get());             
        System.out.println("optional.orElse(\"Posa\") : " + optional.orElse("Posa"));    
        optional.ifPresent((s) -> System.out.println(s.charAt(0)));   
}

Output:

optional.isPresent() : true
optional.get() : Rams
optional.orElse("Posa") : Rams
R

Example-2:-

package com.way2it.java8.optional;
import java.util.Optional;
public class OptionalTest2 {
public static void main(String[] args) {
        Optional<String> optional = Optional.empty();
        System.out.println(optional);
        System.out.println(optional.isPresent());
     
        Optional<String> optional2 = Optional.ofNullable(null);
        System.out.println(optional2);
        System.out.println(optional2.isPresent());
     
        String value = "Rams";
        Optional<String> optional3 = Optional.ofNullable(value);
        System.out.println(optional3);
        System.out.println(optional3.isPresent());
    }
}

Output:

Optional.empty
false
Optional.empty
false
Optional[Rams]
true