:::: MENU ::::

Features of Java 8

Programming, Software development

Features of Java 8



Java 8 is released on 18 march 2014. It is the most awaited & major release of Java. There are dozens of new features added to Java Programming Language with this release. Along with these new features, lot of feature enhancements have been done under-the-hood, at both compiler and JVM level. Lets take a look at interesting language features.

Lambda Expression (λ)

  • A lambda expression is a block of code that you can pass around so it can be executed later, just once or multiple times.
  • Lambda expression facilitates functional programming.
  • It simplifies the development a lot.
  • Important characteristics of lambda expression:
    • Optional type declaration
    • Optional parenthesis
    • Optional curly braces
    • Optional return keyword
parameter -> expression body
(a, b)  ->  a + b;


Functional Interfaces

  • Functional interfaces have a single functionality to exhibit.
  • An interface is a functional interface if it defines exactly one abstract method.
  • A new annotation, @FunctionalInterface, has been introduced.
  • Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions in java.util.function package.
  • A lambda expression can be supplied whenever an object of an interface with a single abstract method is expected.


Method References

  • Method reference allows to reference constructors or methods without executing them.
  • Method references help to point to methods by their names.
  • A method reference is described using :: (double colon) symbol.
  • Method reference is used when lambda expression does nothing but call and existing method.
  • A method reference can be used to point the following types of methods:
    • Static methods
    • Instance methods
    • Constructors using new operator 
x -> System.out.println(x);

(x, y) -> x.compareToIgnoreCase(y);


Default Methods

  • Default method provides default implementation of a methods in interfaces.
  • Default methods enable to add new functionalities to interfaces without breaking the classes that implements that interface.
  • Default methods provide the most elegant and practical way to allow backwards compatibility.
  • This capability is added so that old interfaces can be used to leverage the lambda expression capability of Java 8.
  • Interfaces can have static default methods as well.
interface Person {
     default String getName() { return “Om"; }




  • Stream represents a sequence of objects from a source, which supports many parallel operations to process the data, while completely abstracting out the low level multithreading logic.
  • A stream gets/computes elements on demand. It never stores the elements.
  • Stream takes Collections, Arrays, or I/O resources as input source.
  • Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.
  • Most of the stream operations return stream itself so that their result can be pipelined.
  • Collection interface has two methods to generate a Stream.
    • stream() − Returns a sequential stream considering collection as its source.
    • parallelStream() − Returns a parallel Stream considering collection as its source.
List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl"); 

List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());



  • Optional is a container object which may or may not contain a non-null value.
  • Optional object is used to represent null with absent value.
  • This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values.
  • Optional class provides variety of methods to cure some of the problems of NullPointerExceptions.
  • You can use lambda expression while using Optional class.
Optional optionalString=Optional.ofNullable(optionalTest.getNullString()); 



Nashorn JavaScript

  • Nashorn, a much improved javascript engine is introduced in JAVA 8 similar to V8 engine provided by chrome over which Node.js runs.
  • It enables us to run javascript to run on a  jvm.
  • It is compatible with Node.js applications while also allowing actual Java libraries to be called by the javascript code running on server.
  • JAVA 8 introduces a new command line tool, jjs, to execute javascript codes at console.
  • Using ScriptEngineManager, JavaScript code can be called and interpreted in Java.


New Date/Time API

  • Java 8 introduces a new date-time API under the package java.time.
  • It allows easy timezone handling with help of domain specific design.
  • It introduced two important super classes to deal with problems.
  • Local: Simplified date-time API with no complexity of time zone handling.
  • Zoned: Specialized date-time API to deal with various time zones.
  • New API now supports Joda Time.
  • Most of the classes are immutable and threadsafe.
  • The new API prefers enums over integer constants for things like months and days of the week.
ZoneId zone = ZoneId.systemDefault(); 
Clock clock = Clock.system(zone); 

long time = clock.millis();

Leave a comment

I am reachable Contact me