Java - New in Java 9 - Interview Questions

Java 9 is a major release of Java which introduced the concept of Java platform module system. In addition Java 9 includes enhancements to the Java core libraries, JVM tuning, Security, Deployment and JDK Tools.

Since Java 9 is a major release after Java 8, questions on the features added in Java 9 are frequently asked in Java interviews. This is usually to check if you are up-to-date with the latest changes in Java programming language.

What are the key features and enhancements introduced in Java 9?

 FAQ

Java 9 is a major enhancement release of the platform containing enhancements to the Java core libraries, JVM, security, tools etc. Following are some of the key features and enhancements introduced in Java 9.

1. Java Platform Module System

2. Factory Methods for Collections

3. Private methods in Interfaces

4. Improvements to Streams API

5. New tool JShell

6. New tool JLink

7. Support for HTTP/2

Describe the Java platform module system introduced in Java 9?

 FAQ

Java 9 introduced a new concept and component called 'Module' which adds a higher level of aggregation over packages. A module consists of a group of related packages and resources which are reusable. In addition, the module specifies the list of external modules that it is dependent on, and also list of its packages that it makes available to other modules.

Java 9 divides the JDK into several modules that support different configurations. The Java command 'java --list-modules' lists out all the modules in the JDK. Modularization in JDK enables to greatly reduce the runtime size of Java application. For example, if you have a java application for IoT devices and does not have the need for Java GUI classes, then you can create a specific runtime that does not include the GUI modules.

How do you declare a Module?

 FAQ

A module declaration begins with the keyword 'module' followed by the module name and curly braces {..} for the body. The module body can contain various module directives such as requires, exports, uses etc.

Following code snippet highlights the module declaration syntax.

module moduleA {
 //module dependency
 requires moduleB;

 //packages made available to other modules
 export packageA;
 export packageB;}

How do you declare a Module?

 FAQ

A module is defined in a module declaration file, which is a file named 'module-info.java' in the project’s root folder. The module declaration gets compiled into module-info.class in the JAR’s root. This is called the module descriptor.

A module has three properties - name, dependencies and exports

A module declaration begins with the keyword 'module' followed by the module name and curly braces {..} for the body. The module body can contain various module directives such as requires, exports, uses etc.

Following code snippet highlights the module declaration syntax.

module moduleA {
 //module dependency
 requires moduleB;

 //packages made available to other modules
 export packageA;
 export packageB;}

Can you have two modules with the same name?

 FAQ

No, module names have to be unique. if multiple modules have the same name at compile time, a compilation error occurs. If multiple modules have the same name at runtime, an exception occurs.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

Can a module and a package within that module have the same name?

 FAQ

Yes, a module and a package within that module have the same name.

What factory methods were added for collections in Java 9?

 FAQ

Java 9 added static factory methods for collections that can be used to easily create small immutable collections with minimum lines of code.

Examples of these methods are List.of(), Set.of() and Map.of().

List.of('US','UK','CA')
Set.of('US','UK','CA')
Map.of('US','United States','UK','United Kingdom','CA',''Canada)

What changes to interfaces were introduced in Java 9?

 FAQ

In Java 9 we can define private and private static methods. These methods can be used by other methods in the interface, hence it prevents code duplication and enables code reuseability. By defining them as private we prevent the sub-classes from accessing these methods.

Private methods must contain a body and they cannot be abstract.

Prior to Java 7 Interfaces could contain only 'public abstract' methods.

Since Java 8, in addition to 'public abstract' methods you could also have 'public static' methods and 'public default' methods.

Since Java 9, in addition to 'public abstract', 'public static' and 'public default' methods; you can have 'private' methods. Private methods can be called from static methods and default methods from within the interface. private methods in interfaces are not accessible to the implementation class.

public interface MyInterface {

 //abstract method
 public abstract void method1();

 //default method
 public void method2() {
  ...
 }

 //static method
 public void method3() {
  ...
 }

 //private method
 private void method4() {
  ...
 }

}

What new features were added in the Streams API in Java 9?

 FAQ

Java 9 introduced the following new features to the Streams API

1. takeWhile(Predicate p): If the stream is ordered, then this method returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate. If the stream is unordered, then this method returns a stream consisting of a subset of elements taken from this stream that match the given predicate.

2. dropWhile(Predicate p): If the stream is ordered, then this method returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate. If this stream is unordered, then this method returns a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate.

3. iterate():

4. ofNullable(): This method returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.

What is JShell?

 FAQ

JShell is an interactive REPL (Read-Eval-Print-Loop) tool provided in Java 9 which is useful for learning the Java programming language and prototyping Java code. You can launch JShell from the console and directly type and execute Java code. The immediate feedback of JShell makes it a great tool to explore new APIs and try out language features.

To start JShell, enter the command 'jshell' on the command line.

% jshell
| Welcome to JShell -- Version 9
| For an introduction type: /help intro

jshell>

To exit JShell enter the command '/exit' on the command line.

jshell> /exit
| Goodbye

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

What is JLink?

 FAQ

JLink is Java’s new command line tool through which you can create you own customized JRE by linking sets of modules (and their transitive dependencies) to create a run-time image.

JLink will allow you to both simplify and reduce the size of deployment.

Support for HTTP/2 in Java 9?

 FAQ

Java 9 introduced new class that handles http/2 connections.

These classes are

1. HttpClient - which handles the creation and send of requests.

2. HttpRequest which is used to construct a request to be sent via the HttpClient.

3. HttpResponse which holds the response from the request that has been sent.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example