Prev Next

Java / Java 11

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is the Java Flight Recorder tool introduced in Java 11?

Java Flight Recorder (JFR) is a Java profiling tool that monitors and diagnoses any running Java application. It is responsible for collecting data about the running environment, JVM, and Java application and dumps the recorded data into a .jfr file, and which we can consume using Java Mission Control (JMC) to analyze and visualize the .jfr file.

2. What is Java Mission Control in Java 11?

Java Mission Control is an application that can analyze the dumps that come from Java Flight Recorder, and give you a graphical overview of what is happening inside of a JVM.

3. List some of the Java 11 Features.

Java 11 is the first long-term support (LTS) release after Java 8. Oracle also stopped supporting Java 8 in January 2019. So it becomes mandatory for the application teams to onboard to Java 11 ASAP.

some of the new features include,

new methods/API to the String class: isBlank, lines, strip, stripLeading, stripTrailing, and repeat.

easier way to read and write Strings from files using readString and writeString static methods from the Files class.

Path filePath = Files.writeString(Files.createTempFile("tempDIR", "demo", ".txt"), "Javapedia.net");
String fileContent = Files.readString(filePath);
System.out.println(fileContent); // prints Javapedia.net

The java.util.Collection interface adds a new default toArray method which makes it easy to create an array from a collection using its right data type.

List myListCollection = Arrays.asList("orange", "Apple");
String[] convertedArray = myListCollection.toArray(String[]::new);
System.out.println(convertedArray);

addition of Predicate.not method like Predicate.not(String::isBlank)),

addition of support for using the local variable syntax (var keyword) in lambda parameters,

The new HTTP client from the java.net.http package was introduced in Java 9. It has now become a standard feature in Java 11.

We don't have to compile the Java source files with javac explicitly anymore, we can directly run the file using the java command:

>java HelloWorld.java
Hello world!

Nest-Based Access Control.

Java 11 introduced the Z Garbage Collector (ZGC).

4. What is the Z Garbage Collector (ZGC) in Java 11?

The Z Garbage Collector (ZGC) is a scalable low latency garbage collector. ZGC performs all expensive work concurrently, without stopping the execution of application threads for more than 10ms, which makes it suitable for applications that require low latency and/or use a very large heap (multi-terabytes).

The Z Garbage Collector is available as an experimental feature and is enabled with the command-line options -XX:+UnlockExperimentalVMOptions -XX:+UseZGC.

5. Difference between Oracle JDK and OpenJDK.

The main difference between Oracle JDK and OpenJDK is that Oracle JDK incorporates some commercial features. Rather than removing these features to get conversions between Oracle JDK and OpenJDK, Oracle has committed to open source these commercial features into OpenJDK.

Until Java 10, you could use both OpenJDK and Oracle JDK in production free of charge. However, you have to purchase Oracle JDK support for Java 11 onward to run in production.

6. Mention few deprecated modules in Java 11.
  • Nashorn JavaScript Engine,
  • Pack200 compression scheme for JAR files.
7. What is Nest-Based Access Control feature in Java 11?

Nest-Based Access Control, supports private member access within nest members directly, without the need of any auto-generated bridge method.

The change is backward compatible, which requires no code change, it is just a Java compiler's optimization to remove the bridge method access internally.

8. What is String.lines() API in Java 11?

The lines() method is a static method that returns a stream of lines extracted from a given multi-line string, separated by line terminators.

public Stream<String> lines()

The stream returned by lines() method contains the lines from this string in the same order in which they occur in the multi-line.

import java.util.ArrayList;
import java.util.List;
 
public class JavaStringLinesAPIExample {
 
    public static void main(String[] args) {
        String inputStr = "\nOne\nTwo\nThree\r\nFour\n";
 
        List<String> lines = new ArrayList<>();
        inputStr.lines().forEach(s -> lines.add(s));
 
        System.out.println(lines);
    }
 
}

9. Explain String strip API in Java 11.

String class includes 3 more methods which help in removing extra white-spaces in Java 11.

  • String strip() returns a string whose value is given string, with all leading and trailing white space removed. This API is similar to String.trim() method.
  • String stripLeading() returns a string whose value is given string, with all leading white space removed.
  • String stripTrailing() returns a string whose value is given string, with all trailing white space removed.
public class StringStripDemo
{
    public static void main(String[] args) 
    {
        String str = "  Javapedia.net !   ";
 
        System.out.println( str.strip() );          //prints "Javapedia.net !"
 
        System.out.println( str.stripLeading() );   //prints "Javapedia.net !   "
 
        System.out.println( str.stripTrailing() );  //prints "  Javapedia.net !"
    }
}

10. What are compact Strings in Java?

Since Java 9, the JVM optimizes strings by using a new feature called Compact Strings. Instead of having a char[] array, a string can be represented as a byte[] array. We can use either UTF-16 or Latin-1 to produce either one or two bytes per character. If JVM detects the string contains only ISO-8859-1/Latin-1 characters, then string uses one byte per character internally.

This feature is enabled by default and switches off using the -XX:-CompactStrings.

«
»
Java 12

Comments & Discussions