New Features in Java 8

Java 8 brought some of the interesting features in Java. In this article, we will cover new features in Java 8.

 

1. Functional Interfaces in Java 8

Function Interface introduced in Java8 as new features along with Stream API, Time API, Lambda Expression, etc., Function Interface having only one abstract method which means interface contains only one abstract method and it can have multiple default methods.@FunctionalInterface annotation introduced along with other annotation in Java 8 which can be used to validate at the compile time that can’t have more than one method beside default.

For the complete tutorial, Read Functional Interface Example Tutorial.

 

2. Java 8 Optional<T>

Java Optional class introduced in Java 8. As a Java developer, You must have faced NullPointerException at least once.NullPointerExceptions are a pain but the truth is you can not avoid it but can add certain checks to make sure we are ready for it when it happens. A common (bad) practice is to return the null reference to indicate the absence of a given object and which can cause NullPointerException. The Optional class introduced in Java8 to handle optional values instead of null reference.

For the complete tutorial, Read Optional API Example Tutorial.

 

3. Java 8 StringJoiner

Joining multiple strings in a very common task in day-to-day programming activity. There was no direct way to join multiple String in Java (Other than using the third-party API’s). Java 8 Introduced a new class StringJoiner which is useful to join multiple Strings.StringJoiner is a kind of a Collector.

For the complete tutorial, Read StringJoiner Example Tutorial.

 

4. Stream API

Streams are one of the most important APIs introduced in Java 8 and completely changes the way we do processing of Collections in Java. This is in line with the theme of Java since past few versions where advances in hardware got exploited by the language.

For more detail, please read Introduction to Java 8 Streams.

 

5. Java 8 Date Time

Java 1.0 introduced support for date and time using java.util.Date class. This API has a lot of issues including not representing the date as a human-readable date but time with milliseconds. We will cover some of the issues or drawbacks of the existing Date API which made way for the Java 8 Date Time.

For the complete tutorial, Read Java Date Time API Example Tutorial.

 

6. Default and Static Methods

Before Java 8, we had the only option of public abstract methods. This was a major limitation since we can not add new functionalities to the existing interface without forcing all subclasses to implement new method introduced in the interface.Java 8 introduced concept of static and default methods at the interface level to handle some of the ground-breaking changes introduced in Java 8.

6.1 Static Method

public interface ExampleInterface {
 public static void method() {
  System.out.println("hello world");
 }
}

The static method belongs only to Interface class, so you can only invoke a static method on Interface class, not on the class implementing this Interface.

public class TestClass implements ExampleInterface {

 public static void main(String[] args) {
  TestClass.staticMethod(); //not valid - static method may be invoked on containing interface class only
  TestClass.staticMethod(); //valid
 }
}

 

6.2 Default Method

Default methods are declared using the new default keyword. Default methods can be overridden in implementing a class.

public interface ExampleInterface {

 public default void menthod() {
  System.out.println("default print");
 }
}

For the complete tutorial, Read Default Methods in Java

Summary

In this article, We covered some of the exciting new features in Java 8. We have only covered core features introduced in the Java 8.

2 thoughts on “New Features in Java 8”

  1. A very Nice and simple explanation of each feature in Java 8
    Thank you for sharing.
    The tutorial covered all the major features with very simplicity.

  2. Great article! I want to start career in java programming and so was looking for ‘what makes java different from other languages in features’. Here found your article very interesting and helpful as java beginner. Thanks a lot for sharing java 8 features with us!

Comments are closed.

Scroll to Top