Java 11 Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Java 11?
Java 11 is the second release of the long-term support (LTS) after Java 8. Since Java 11, Oracle JDK is no longer free for commercial use. You can use it in the development stages, but you need to buy a license to use it commercially. After Java 11, Oracle will not provide free long-term (LTS) support for any single Java version.
Ques 2. What is the difference between Oracle JDK and OpenJDK?
Ever since Java 7, Java has been developed out in the open in the OpenJDK project. It's open source, but it's mainly driven by committers from Oracle, the owners of Java. But there are also many outside contributions from the likes of Red Hat, Twitter, and IBM. When you want to run Java, you have a choice to either use the OpenJDK releases or to use the Oracle JDK release. The Oracle JDK release used to be a slightly extended and amended version of OpenJDK. The ultimate goal here is to have no differences between the Oracle JDK builds and the OpenJDK builds.
The main difference between Oracle JDK and OpenJDK is that Oracle JDK incorporates some commercial features. Rather than stripping these features to get conversions between Oracle JDK and OpenJDK, Oracle has committed to open source these commercial features into OpenJDK. This process already started with Java 9 and 10 and is continued with Java 11.
With Java 11, the goal of a convergence between the Oracle JDK and OpenJDK code bases have been achieved. there's some pretty big differences in licensing. OpenJDK is GPL 2 licensed, so it has a true open source license. Oracle JDK, on the other hand, has a proprietary license called the Oracle Binary Code License Agreement. Up until Java 10, you could use both OpenJDK and Oracle JDK in production free of charge. For Oracle JDK, you could buy optional support from Oracle. That's changing with Java 11. For OpenJDK there are no changes, you can still use the GPL 2 license builds. However, you can no longer use Oracle JDK free of charge in production.
Ques 3. Which commercial features are available as open source in Java 11?
One of the Oracle JDK commercial features that have been open-sourced is Java Flight Recorder. In addition to Java Flight Recorder, Java Mission Control has also been open sourced.
Ques 4. How can I download the free version of Java 11?
You can download from Java 11. Download tar/zip, unzip them, install and set the environment variables to use java 11.
Ques 5. What are Java 11 Features?
Below is a list of the main features included in Java 11 :
- Nest-Based Access Control
- Dynamic Class-File Constants
- Improve Aarch64 Intrinsics
- Epsilon: A No-Op Garbage Collector
- Remove the Java EE and CORBA Modules
- HTTP Client (Standard)
- Local-Variable Syntax for Lambda Parameters
- Key Agreement with Curve25519 and Curve448
- Unicode 10
- Flight Recorder
- ChaCha20 and Poly1305 Cryptographic Algorithms
- Launch Single-File Source-Code Programs
- Low-Overhead Heap Profiling
- Transport Layer Security (TLS) 1.3
- ZGC: A Scalable Low-Latency Garbage Collector(Experimental)
- Deprecate the Nashorn JavaScript Engine
- Deprecate the Pack200 Tools and API
Ques 6. What is Nest-Based Access Control in Java 11?
Java allows classes and interfaces to be nested within each other. These nested forms have unlimited access to each other, including private fields, methods, and constructors.
Nests allow nested classes, which are part of the same enclosing class but are compiled into different class files, to access each other\'s private members without the need for compilers to insert synthetic accessibility-broadening bridge methods. This is a Java class bytecode level change.
public class TestOuter {
public void testingOuterPublic() {
}
private void testingOuterPrivate() {
}
class TestInner {
public void testingInner() {
testingOuterPrivate();
}
}
}
TestOuter and TestInner form a nest together, they are each other\'s nestmates.
The method testingOuterPrivate() can be accessed inside inner class, even tough testingOuterPrivate() method is private.
If we compile above class, will get compilation error. This is because of the reason that the outer and nested classes are compiled to different files and they need package-private visibility to access each other\'s private members. JVM access rules do not permit private access between nestmates.
Java 11 provides JVM level support for private access through the \"NestMembers\" and \"NestHost\" attributes within outer / nested classes.
NestMembers
corresponds to nested classes.NestHost
corresponds to the enclosing outer class. Now these attributes connect outer and nested classes, rather than package-private bridge methods created synthetically.
Using Reflection:
package com.withoutbook;
import java.lang.reflect.Field;
public class TestOuter {
private static int testingNumber = 17;
public static class NestedInnerTest {
public static void test() throws Exception {
Field value = TestOuter.class.getDeclaredField(\"testingNumber\");
value.setInt(null, 12);
}
}
public static void main(String[] args) throws Exception {
NestedInnerTest.test();
System.out.println(TestOuter.testingNumber);
}
}
If we run above code in Java 10, will get below exception:
Exception in thread \"main\" java.lang.IllegalAccessException:
class com.techgeeknext.TestOuter$NestedInnerTest cannot access a member of class com.techgeeknext.controller.TestOuter with modifiers \"private static\"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:639) at java.base/java.lang.reflect.Field.checkAccess(Field.java:1075)
at java.base/java.lang.reflect.Field.setInt(Field.java:958) at com.techgeeknext.controller.TestOuter$NestedInnerTest.test(TestOuter.java:12)
at com.techgeeknext.controller.TestOuter.main(TestOuter.java:18)
Whereas in Java 11, it will run successfully without exception with below output:
12
Most helpful rated by users: