Found 2616 Articles for Java

Importance of Thread.onSpinWait() method in Java 9?

raja
Updated on 31-Mar-2020 11:24:07

689 Views

Thread.onSpinWait() method has been introduced in Java 9. It is a static method of Thread class and can be optionally called in busy-waiting loops. It allows the JVM to issue processor instructions on some system architectures to improve reaction time in such spin-wait loops, and also reduce the power consumed by the core thread. It can benefit the overall power consumption of a java program and allows other core threads to execute at faster speeds within the same power consumption envelope.Syntaxpublic static void onSpinWait()Examplepublic class ThreadOnSpinWaitTest {    public static void main(final String args[]) throws InterruptedException {       ... Read More

How to list all the classes, interfaces, and enums in JShell in Java 9?

raja
Updated on 31-Mar-2020 08:47:01

311 Views

The JShell tool also called REPL(Read-Evaluate-Print-Loop) introduced in Java 9 that allows us to execute Java code and getting immediate results. We can quickly evaluate expressions or short algorithms without creating a new project, compile or build it. With the help of JShell, we can execute expressions, use imports, define classes, methods, and variables.We can list out all the classes, interfaces and enums defined in the current JShell session by using "/types" command.In the below code snippet, we have created the "Test" class, "TestInterface" interface, and enum "EnumTest" in the JShell tool.C:\Users\User> jshell | Welcome to JShell -- Version 9.0.4 | For ... Read More

When can we use StackWalker.getCallerClass() method in Java 9?

raja
Updated on 30-Mar-2020 15:26:41

218 Views

Java 9 has provided an efficient way of stack walking for lazy access, filtering stack trace using StackWalker API. An object of StackWalker can allow us to traverse and access to stacks. This class contains some useful methods like walk(), forEach(), and getCallerClass().The getCallerClass() method returns the class that invokes the method that calls this method. To get hold of calling class instance, we need RETAIN_CLASS_REFERENCE while getting StackWalker instance. RETAIN_CLASS_REFERENCE retains an instance of all classes walked by StackWalker.Syntaxpublic Class getCallerClass()Exampleimport java.lang.StackWalker.Option; public class StackWalkerTest {    public static void main(String args[]) {       StackWalkerTest1.test1();   ... Read More

Differences between Compact Strings and Compressed Strings in Java 9?

raja
Updated on 30-Mar-2020 11:50:08

156 Views

Compact Strings have introduced in Java 9 to replace Java 6's Compressed Strings. Its implementation uses byte[] array instead of char[] array and a new field coder has introduced to identify whether it is LATIN1 or UTF16 format while Compressed Strings have introduced in Java 6 that can be used byte[] array for one byte per character, and continued to use char[] array for two bytes per character, previously it can be turned on using -XX: + UseCompressedStrings.Unlike Compressed Strings, Compact Strings do not require un-packing or re-packing. Hence Compact String gives better performance at runtime.Compressed Strings are not enabled by default in Java 6, and need to be ... Read More

What is the importance of REPL in Java 9?

raja
Updated on 27-Mar-2020 17:04:10

497 Views

REPL stands for Read-Eval-Print-Loop. It is a shell where the user types an expression, it's evaluated, and the result returned to the user. The main purpose of using REPL is to interact quickly with Java programs without creating a java file, compile it, and run it. JShell is very useful for the developers and allows us to learn the Java language.Below are some of the features of REPLIt’s built-in in Java 9.We can test any Java expression without a class file, compiling and running it.It autocompletes the methods, just typing the TAB key, as in your editor.We can define methods, and ... Read More

How to get a stream from Optional class in Java 9?

raja
Updated on 27-Mar-2020 13:02:36

98 Views

The Optional class provides a container that may or may not contain a non-null value. It has been introduced in Java 8 to reduce the number of places in the code where a NullPointerException has generated. Java 9 added three methods: ifPresentOrElse(),  or(), and stream(), which helps us deal with default values.In the below example, we can get a stream from Optional class using Person classExampleimport java.util.Optional; import java.util.stream.Stream; public class OptionalTest {    public static void main(String args[]) {       getPerson().stream()                  .map(Person::getName)                 ... Read More

How to get Java and OS version, vendor details in JShell in Java 9?

raja
Updated on 27-Mar-2020 11:40:08

408 Views

Java 9 introduces the JShell tool for the Java Programming Language. This tool allows us to evaluate code snippets such as declarations, statements, and expressions. We will get the details about Java Version and Vendor, and also get the details about OS Version and Name by using the static method: getProperty() of System class.In the below code snippet, we can able to get the details of Java version, Java Vendor and OS version and OS name in JShell console.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> System.out.println(System.getProperty("java.version")); 9.0.4 jshell> System.out.println(System.getProperty("java.vendor")); Oracle ... Read More

How to implement reactive streams using Flow API in Java 9?

raja
Updated on 27-Mar-2020 08:22:11

2K+ Views

Flow API is official support for reactive streams specification since Java 9. It is a combination of both Iterator and Observer patterns. The Flow API is an interoperation specification and not an end-user API like RxJava.Flow API consists of four basic interfaces:Subscriber: The Subscriber subscribes to Publisher for callbacks.Publisher: The Publisher publishes the stream of data items to the registered subscribers.Subscription: The link between publisher and subscriber.Processor: The processor sits between Publisher and Subscriber, and transforms one stream to another.In the below example, we have created a basic subscriber that asks for one data object, prints it and asks for one more. ... Read More

How to create a thread in JShell in Java 9?

raja
Updated on 27-Mar-2020 06:50:00

157 Views

JShell is an interactive java shell tool introduced in Java 9 and allows us to execute code snippets, and shows the result immediately without declaring the main() method like Java. It is a REPL (Read-Evaluate-Print- Loop) tool and runs from the command-line prompt. We can create variables, methods, classes, scratch variables, external libraries, and etc using JShellIn the below code snippet, we can create a thread by extending the Thread class.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> class ThreadTest extends Thread { ...>       public void run() { ...> ... Read More

What is the purpose of using JLink in Java 9?

raja
Updated on 26-Mar-2020 14:25:18

108 Views

The main purpose of the JLink feature is to create our own Customized JRE. Usually, we run a program with the default JRE that has been provided by Oracle Corporation with 214 MB of size.For instance, a user wants to print a simple "Hello World" message as shown belowpublic class HelloWorldModuleTest {    public static void main(String args[[]) {       System.out.println("Hello World!");    } }To run the above program of 1 KB size, we need 4 – 5 classes like String, System, Object, and HelloWorldModuleTest.class files. Then why do we need to load 214 MB of JRE using ... Read More

Advertisements