The Important Features In Java 8

Posted By : Ankit Shriwastav | 28-Jun-2023

html Java Java Virtual Machine

Loading...

On March 18, 2014, Java 8 was released. With various enhancements made to Java programming, JVM, libraries, and Tools, it was a revolutionary release for the software development platform. Some of the important and useful Java 8 features are as follows:-

Functional Interfaces

A new concept was introduced in Java 8 as Functional Interface. Any interface becomes a Functional interface when it has exactly one abstract method. But this doesn't limit the number of default or static methods that a functional interface can have. Methods of an Object class(Example:- toString()) can also be declared by functional interface.


@FunctionalInterface annotation is not used to mark an interface as Functional Interface but this annotation provides a facility that helps us to avoid accidental addition of abstract methods. Method run() in java.lang.Runnable is a great example of a functional interface. The instance of a functional interface can be represented by Lambda expressions.

Also, ReadSorting With Lambdas In Java



1) Lambda Expressions

By using an expression, it helps with a way to implement Functional Interface. Since only one method is present in the functional interface, lambda expression helps us in method implementation by passing method arguments and business logic. It helps to iterate, filter and extract data in collection libraries.

For Example:-

Runnable r = new Runnable(){
@Override
public void run() {
System.out.println(“My Example”);
}};

With the help of Lambda expressions, the above code can be written in just a single line :

Runnable r1 = () -> { System.out.println(“My Example”); };

The compiler doesn't create a .class file separately because Lambda expressions are treated as a function.

2) Method References

To point methods by their names, method reference is used. :: symbol is used to describe method references. This symbol can be used with instance methods or static methods and with constructors by using a new operator like TreeSet::new. The shorthand notation of lambda expression to call a method is Method reference.

str -> System.out.println(str)

The above lambda expression can be replaced with method reference as follows:-

Example :

public class Xyz {
public static void thread(){
System.out.println(“Thread is running !!!!!”);
}
public static void main(String[] args) {
Thread t2=new Thread(Xyz::thread);
t2.start(); }}

3) forEach

To iterate over Streams and Collections, a new method was provided by Java 8. It is also defined in Stream interface and Iterable. Whenever it is required to traverse through a Collection, there is a need to create an Iterator and during the process, we may get ConcurrentModificationException if the iterator is not being used properly.


Those Collection classes that extend the Iterable interface can use forEach() method so as to iterate elements. This method intakes only one parameter which is a functional interface. So, a lambda expression can be passed as an argument.

For Example:-

public static void main(String[] args) {
List<String> list= new ArrayList<String>();
list.add(“Mango”);
list.add(“Pomogranate”);
list.add(“Pear”);
//lambda expression in forEach Method
list.forEach(str->System.out.println(str));
//above line can also be written using method reference as
list.forEach(System.out::println));
}

Also, ReadPros and Cons of Updating a Project From Java 8 to Java 17

4) Stream API

Stream was introduced in Java 8. With the help of Streams, just like SQL statements, we can process data in a declarative way. It acts for a number of objects from a source that supports aggregate functions. Parallel execution as well as sequential execution both are allowed in Stream API.

Example for Stream filter :

public static void main(String[] args) {
List<Product> product = new ArrayList<Product>();
//Adding Products
product.add(new Product(1,”Vegetables”,30000f));
product.add(new Product(2,”Automobile”,34000f));
product.add(new Product(3,”Cutllery”,25000f));

List<Float> price =products.stream()
.filter(p -> p.price > 30000)
.map(p->p.price)
.collect(Collectors.toList());
System.out.println(price);
}

5) Base64 Encoding and Decoding

Base64 Encoding and Decoding is one more feature that was introduced in Java 8. To use its methods, we need to import java.util.Base64 class in our source file.
Before Java 8, for encoding and decoding external jars like Apache Commons Codec’s Base64.encodeBase64(byte[]) and Base64.decodeBase64(byte[]), or Sun’s internal base64 encoder and decoder, sun.misc.BASE64Encoder().encode() and sun.misc.BASE64Decoder().decode() needed to be added.

Conclusion

In this blog post, we got to learn some of the important features of Java 8. Hope you found it helpful!