Java SE 8 Interview Questions and Answers - 1
Contents
- 1 Java SE 8 New Features?
- 2 Advantages of Java SE 8 New Features?
- 3 What is Lambda Expression?
- 4 What are the three parts of a Lambda Expression? What is the type of Lambda Expression?
- 5 What is the type of the following Lambda Expression?
- 6 What is a Functional Interface? What is SAM Interface?
- 7 Is is possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?
- 8 Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?
- 9 When do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?
- 10 Explain Differences between Collection API and Stream API?
- 11 What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8?
- 12 What is Optional in Java 8? What is the use of Optional?Advantages of Java 8 Optional?
- 13 What is Type Inference? Is Type Inference available in older versions like Java 7 and Before 7 or it is available only in Java SE 8?
Java SE 8 New Features?
- Lambda Expressions
- Functional Interfaces
- Stream API
- Date and Time API
- Interface Default Methods and Static Methods
- Spliterator
- Method and Constructor References
- Collections API Enhancements
- Concurrency Utils Enhancements
- Fork/Join Framework Enhancements
- Internal Iteration
- Parallel Array and Parallel Collection Operations
- Optional
- Type Annotations and Repeatable Annotations
- Method Parameter Reflection
- Base64 Encoding and Decoding
- IO and NIO2 Enhancements
- Nashorn JavaScript Engine
- javac Enhancements
- JVM Changes
- Java 8 Compact Profiles: compact1,compact2,compact3
- JDBC 4.2
- JAXP 1.6
- Java DB 10.10
- Networking
- Security Changes
Advantages of Java SE 8 New Features?
we can get the following benefits from Java SE 8 New Features:
- More Concise and Readable code
- More Reusable code
- More Testable and Maintainable Code
- Highly Concurrent and Highly Scalable Code
- Write Parallel Code
- Write Database Like Operations
- Better Performance Applications
- More Productive code
What is Lambda Expression?
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.
What are the three parts of a Lambda Expression? What is the type of Lambda Expression?
A Lambda Expression contains 3 parts:
- Parameter List
A Lambda Expression can contain zero or one or more parameters. It is optional.
- Lambda Arrow Operator
“->” is known as Lambda Arrow operator. It separates parameters list and body.
- Lambda Expression Body
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.
What is a Functional Interface? What is SAM Interface?
A Functional Interface is an interface, which contains one and only one abstract method. Functional Interface is also know 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.
Is is possible to define our own Functional Interface? What is @FunctionalInterface? What are the rules to define a Functional Interface?
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:
- Define an interface with one and only one abstract method.
- We cannot define more than one abstract method.
- Use @FunctionalInterface annotation in interface definition.
- We can define any number of other methods like Default methods, Static methods.
- If we override java.lang.Object class’s method as an abstract method, which does not count as an abstract method.
Is @FunctionalInterface annotation mandatory to define a Functional Interface? What is the use of @FunctionalInterface annotation? Why do we need Functional Interfaces in Java?
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 do we go for Java 8 Stream API? Why do we need to use Java 8 Stream API in our projects?
When our Java project wants to perform the following operations, it’s better to use Java 8 Stream API to get lot of benefits:
- When we want perform Database like Operations. For instance, we want perform groupby operation, orderby operation etc.
- When want to Perform operations Lazily.
- When we want to write Functional Style programming.
- When we want to perform Parallel Operations.
- When want to use Internal Iteration
- When we want to perform Pipelining operations.
- When we want to achieve better performance.
Explain Differences between Collection API and Stream API?
COLLECTION API | STREAM API |
It’s available since Java 1.2 | It is introduced in Java SE8 |
It is used to store Data(A set of Objects). | It is used to compute data(Computation on a set of Objects). |
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. |
It is used to store limited number of Elements. | It is used to store either Limited or Infinite Number of Elements. |
Typically, it uses External Iteration concept to iterate Elements such as Iterator. | Stream API uses External Iteration to iterate Elements, using forEach methods. |
Collection Object is constructed Eagerly. | Stream Object is constructed Lazily. |
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. |
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. |
What is Spliterator in Java SE 8?Differences between Iterator and Spliterator in Java SE 8?
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.
SPLITERATOR | ITERATOR |
It is introduced in Java SE 8. | It is available since Java 1.2. |
Splitable Iterator | Non-Splitable Iterator |
It is used in Stream API. | It is used for Collection API. |
It uses Internal Iteration concept to iterate Streams. | It uses External Iteration concept to iterate Collections. |
We can use Spliterator to iterate Streams in Parallel and Sequential order. | We can use Iterator to iterate Collections only in Sequential order. |
We can get Spliterator by calling spliterator() method on Stream Object. | We can get Iterator by calling iterator() method on Collection Object. |
Important Method: tryAdvance() | Important Methods: next(), hasNext() |
What is Optional in Java 8? What is the use of Optional?Advantages of Java 8 Optional?
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 is 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 “null” value.
Main Advantage of Optional is:
- It is used to avoid null checks.
- It is used to avoid “NullPointerException”.
What is Type Inference? Is Type Inference available in older versions like Java 7 and Before 7 or it is available only in Java SE 8?
Type Inference means determining the Type by compiler at compile-time.
It is not 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 right side, but not defined it’s type. Java Compiler automatically infers it’s 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 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.
<soource> ToIntBiFunction<Integer,Integer> add = (a,b) -> a + b; </source>
Here Java Compiler observes the type definition available at left-side and determines the type of Lambda Expression parameters a and b as Integers.