Java 11 Interview Questions and Answers
Prepare for your Interview today!
You can download from Java 11. Download tar/zip, unzip them, install and set the environment variables to use java 11.
Below is a list of the main features included 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.
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
The Java class-file format is now expanding support for a new kind of constant pool, called CONSTANT_Dynamic. The loading of CONSTANT_Dynamic delegates the creation of a bootstrap method. Same as linking an invokedynamic call site delegates linkage to a bootstrap method. Same as, Java 7 which introduces MethodHandle and MethodType entry in constant pool.
Improve the current string and array intrinsic, and added new intrinsics on AArch64 processors for the java.lang.Math sin, cos and log functions. Intrinsics are used to optimize CPU architecture-specific assembly code that is executed for a given method to boost performance, instead of generic Java code. Although most of the intrinsics are already implemented in port AArch64, optimized intrinsics are still lacking for the following java.lang.Math methods:
Most helpful rated by users: