In this post, we are going to discuss some important Java SE 8 Interview Questions with Answers. I will write one more post to discuss the remaining Java SE 8 Interview Questions.
In this section, we will pick up each question from the previous section and answer it with an in-detailed description. If you need any more information and examples, please go through previous Java SE 8 posts available in JournalDEV.
Oracle Corporation has introduced a lot of new concepts in Java SE 8 to introduce the following benefits:
We can get the following benefits from Java SE 8 New Features:
Lambda Expression is an anonymous function which accepts a set of input parameters and returns results. Lambda Expression is a block of code without any name, with or without parameters and with or without results. This block of code is executed on demand.
A Lambda Expression contains 3 parts:
The type of “Journal Dev” is java.lang.String. The type of “true” is Boolean. In the same way, what is the type of a Lambda Expression? The Type of a Lambda Expression is a Functional Interface. Example:- What is the type of the following Lambda Expression?
() -> System.out.println("Hello World");
This Lambda Expression does not have parameters and does return any results. So it’s type is “java.lang.Runnable” Functional Interface.
A Functional Interface is an interface, which contains one and only one abstract method. Functional Interface is also known as SAM Interface because it contains only one abstract method. SAM Interface stands for Single Abstract Method Interface. Java SE 8 API has defined many Functional Interfaces.
Yes, it is possible to define our own Functional Interfaces. We use Java SE 8’s @FunctionalInterface annotation to mark an interface as Functional Interface. We need to follow these rules to define a Functional Interface:
It is not mandatory to define a Functional Interface with @FunctionalInterface annotation. If we don’t want, We can omit this annotation. However, if we use it in Functional Interface definition, Java Compiler forces to use one and only one abstract method inside that interface. Why do we need Functional Interfaces? The type of a Java SE 8’s Lambda Expression is a Functional Interface. Whereever we use Lambda Expressions that means we are using Functional Interfaces.
When our Java project wants to perform the following operations, it’s better to use Java 8 Stream API to get lot of benefits:
S.No. | Collection API | Stream API |
---|---|---|
1. | It’s available since Java 1.2 | It is introduced in Java SE 8 |
2. | It is used to store Data (A set of Objects). | It is used to compute data (Computation on a set of Objects). |
3. | We can use both Spliterator and Iterator to iterate elements. We can use forEach to performs an action for each element of this stream. | We can’t use Spliterator or Iterator to iterate elements. |
4. | It is used to store unlimited number of elements. | Stream API is used to process on the elements of a Collection. |
5. | Typically, it uses External Iteration concept to iterate Elements such as Iterator. | Stream API uses internal iteration to iterate Elements, using forEach methods. |
6. | Collection Object is constructed Eagerly. | Stream Object is constructed Lazily. |
7. | We add elements to Collection object only after it is computed completely. | We can add elements to Stream Object without any prior computation. That means Stream objects are computed on-demand. |
8. | We can iterate and consume elements from a Collection Object at any number of times. | We can iterate and consume elements from a Stream Object only once. |
Spliterator stands for Splitable Iterator. It is newly introduced by Oracle Corporation as part Java SE 8. Like Iterator and ListIterator, It is also one of the Iterator interface.
S.No. | Spliterator | Iterator |
---|---|---|
1. | It is introduced in Java SE 8. | It is available since Java 1.2. |
2. | Splitable Iterator | Non-Splitable Iterator |
3. | It is used in Stream API. | It is used for Collection API. |
4. | It uses Internal Iteration concept to iterate Streams. | It uses External Iteration concept to iterate Collections. |
5. | We can use Spliterator to iterate Streams in Parallel and Sequential order. | We can use Iterator to iterate Collections only in Sequential order. |
6. | We can get Spliterator by calling spliterator() method on Stream Object. | We can get Iterator by calling iterator() method on Collection Object. |
7. | Important Method: tryAdvance() | Important Methods: next(), hasNext() |
Optional: Optional is a final Class introduced as part of Java SE 8. It is defined in java.util package. It is used to represent optional values that are either exist or not exist. It can contain either one value or zero value. If it contains a value, we can get it. Otherwise, we get nothing. It is a bounded collection that is it contains at most one element only. It is an alternative to the “null” value. Main Advantage of Optional is:
Type Inference means determining the Type by compiler at compile-time. It is not a new feature in Java SE 8. It is available in Java 7 and before Java 7 too. Before Java 7:- Let us explore Java arrays. Define a String of Array with values as shown below:
String str[] = { "Java 7", "Java 8", "Java 9" };
Here we have assigned some String values at the right side, but not defined its type. Java Compiler automatically infers its type and creates a String of Array. Java 7: Oracle Corporation has introduced “Diamond Operator” new feature in Java SE 7 to avoid unnecessary Type definition in Generics.
Map<String,List<Customer>> customerInfoByCity = new HashMap<>();
Here we have not defined Type information at the right side, simply defined Java SE 7’s Diamond Operator. Java SE 8: Oracle Corporation has enhanced this Type Inference concept a lot in Java SE 8. We use this concept to define Lambda Expressions, Functions, Method References etc.
ToIntBiFunction<Integer,Integer> add = (a,b) -> a + b;
Here Java Compiler observes the type definition available at the left-side and determines the type of Lambda Expression parameters a and b as Integers. That’s all about Java 8 Interview Questions. I have discussed some Java SE 8 Interview Questions in this post and will discuss some more Java SE 8 Interview Questions in my coming posts. Please drop me a comment if you like my post or have any issues/suggestions.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Good One
- sushil
Good !!!
- Rajib
Nice article, much useful. Just one thing I noticed though. Below would not compile : Integer add = (a,b) -> a + b; Because add must be of type functional interface.
- Rattan
Hi there, I like your site very much, lot to learn! Just one question: Isn’t Stream API the one uses Internal Iteration? In Part2, your first property of Internal Iteration is that it is intruduced in Java 8, so Collections are supposed to iterate Externally, right? Thx
- symbion
Very Good. I like the way you present the questions and Answers straight and clear,
- srao
Nice article to know Java 8 features
- Viswanathan
It’s a great introduction. Thanks
- Ruwan
Nice information, In some mins get to know new concepts of java8 has introduced.
- vishal
Nice one Sir, Keep posting!
- Uma
Very helpful!! Keep it up
- Indiver