Java8 Interview Question

The main feature of Java 8 is to support functional programming. many other languages are supporting functional programming but before java 8, java is only an object-oriented programming language, and we are missing functional programming features. Java community identifies these problems and introduced functional programming in java 8. 

 Q1: List out the Java 8 Features?

Below are the features introduced in Java8


1. Functional Interface
2. Lambda Expressions 
3Default Method
4. Method References
5. Stream API
6. Optional
7. New Date and Time API 
8Nashorn

Q2: What is Functional Interface, What is the use of this?

Functional Interfaces: An interface that contains only a single abstract method (SAM) is called a functional interface. 
->To invoke lambda expression functional interface is required.
->A functional interface can contain any number of default and static methods but It should contain only one abstract method  


@FunctionalInterface: This annotation is used to explicitly declare an interface as a functional interface. It is optional but it is recommended to declare a functional interface with this annotation so the compiler will start checking one and only abstract method, and we can overcome any accidental modification in this interface.


example:
Runnable
Callable
Comparable
Comparator
ActionListner 
Predicate
Function
Consumer 
Supplier 
 


Lambda Expressions: A function/expression without a name is called a lambda expression, we can also call it an anonymous function(without a name, return type, and modifier). The main objective of a lambda expression is to bring the functional programming concept to java.


Why lambda?

->Enable functional programming.
->parallel processing.
->Easier to use API and library.



Default Method:

Up to Java 1.7, every method present inside the interface is always public and abstract. In Java 8, the default and static methods are introduced. The main objective of this method is to provide backward compatibility. 
                                                       Suppose you have an interface and 10 different implementation classes, so if you introduced one more method in the interface then all implemented classes get affected and you need to provide a new implementation in each and every implemented class. to overcome this problem java has introduced a default method inside the interface.


Q: what is the difference between Collection API and Streams API.

truly speaking collection API and streams API are two different things. the collection is used to hold the data but streams are used to process the data, In other words, if you want a group of objects as a single entity, use collection, but if you want to traverse and process the data you can use streams.


Comments