Found 2616 Articles for Java

How can we display all module names in Java 9?

raja
Updated on 19-Mar-2020 14:02:31

262 Views

In Java 9, the module concept has introduced. It is a named, self-describing collection of code and data. The code can be organized as a set of packages containing types like java classes and interfaces, and data includes resources and other kinds of static information. A module contains a name, dependencies, and exported packages.Syntaxmodule com.tutorialspoint.mymodule {    // some statements }In the below example, we can able to display all module names by using the ModuleLayer class.Examplepublic class AllModulesNamesTest {    public static void main(String args[]) {       ModuleLayer.boot().modules().forEach((module) -> {          System.out.println(module.getName());       ... Read More

How to print the pattern of stars in JShell in Java 9?

raja
Updated on 19-Mar-2020 12:10:10

219 Views

JShell is a REPL tool introduced in Java 9 that allows us to execute Java code and getting results immediately. We can evaluate expressions or simple algorithms without creating a new project, compile or build it by using JShell. We can also execute expressions, use imports, define classes, methods, and variables. It is a part of Java 9 JDK but not JRE.We can start JShell session in command-prompt by simply typing jshell. We can use different commands: /exit to quit the JShell session, reset/reload JShell anytime by typing /reset, and /reload,  /import to list the imports, etc.In the below example, we can print ... Read More

What are Compact Strings in Java 9?

raja
Updated on 19-Mar-2020 09:17:42

259 Views

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.The string can be represented with a compact string or not is detected when the string is created. This feature has enabled by default and switches off using the -XX:-CompactStrings. It doesn't revert to a char[] implementation and stores all strings as ... Read More

How to display all stack frames of the current thread in Java 9?

raja
Updated on 19-Mar-2020 07:36:28

256 Views

Stack Walking API can provide a flexible mechanism to traverse and extract information from call stacks that allow us to filter and access frames in a lazy manner. StackWalker class is an entry point to Stack Walking API. The stack trace is a representation of a call stack at a certain point of time in which each element represents a method invocation. It contains all invocations from the start of a thread until the point it’s generated.In the below example, we can print/display all stack frames of the current thread by using StackWalker API.Exampleimport java.lang.StackWalker.StackFrame; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; public ... Read More

How to implement the Fibonacci series in JShell in Java 9?

raja
Updated on 17-Mar-2020 10:51:37

95 Views

JShell is a java shell tool introduced in Java 9 that allows us to execute Java code and prints the result immediately. It is a REPL (Read-Evaluate-Print-Loop) tool that runs from the command-line prompt. A number is said to be the Fibonacci series if each subsequent number is the sum of the previous two numbers.In the below example, we can able to implement the Fibonacci Series in the JShell tool.C:\Users\User\>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> int x=0, y=1, z=0, count=5; x ==> 0 y ==> 1 z ==> 0 count ==> ... Read More

What are new methods added to the String class in Java 9?

raja
Updated on 17-Mar-2020 08:19:14

152 Views

A String is an immutable class in Java and there are two new methods added to the String class in Java 9. Those methods are chars() and codePoints(). Both of these two methods return the IntStream object.1) chars():The chars() method of String class can return a stream of int zero-extending the char values from this sequence.Syntaxpublic IntStream chars()Exampleimport java.util.stream.IntStream; public class StringCharsMethodTest { public static void main(String args[]) { String str = "Welcome to TutorialsPoint"; IntStream intStream = str.chars(); ... Read More

How to get the parent process of the Process API in Java 9?

raja
Updated on 16-Mar-2020 11:51:47

557 Views

ProcessHandle interface allows us to perform some actions, and check the state of a process. It provides the process’s native pid, start time, CPU time, user, parent process, and descendants. We can get access to a parent process by calling the parent() method, and the return value is Optional. It is empty if the child process doesn't have a parent or if the parent is not available.SyntaxOptional parent()Exampleimport java.io.*; public class ParentProcessTest {    public static void main(String args[]) {       try {          Process notepadProcess = new ProcessBuilder("notepad.exe").start();          ProcessHandle parentHandle = notepadProcess.toHandle().parent().get();         ... Read More

How to implement a lambda expression in JShell in Java 9?

raja
Updated on 16-Mar-2020 09:31:21

233 Views

JShell is a Java's first REPL and command-line tool that provides interactive use of Java programming language elements. We can test the functionality in isolation of a class by using this tool. JShell creates a simple and easy programming environment in the command-line that takes input from the user, reads it, and prints the result. A lambda expression is a function that has created without belonging to any class.In the below example, we can implement a lambda expression in JShell.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> Consumer s = (String s) -> System.out.println(s) ... Read More

What is Variable Handle in Java 9?

raja
Updated on 13-Mar-2020 13:47:12

404 Views

Variable Handle is a variable or reference to a set of variables, including other components of a static field, non-static fields, and outer array elements in the heap data structure. It means that Variable Handle is similar to the existing Method Handle. It can be represented by using java.lang.invoke.VarHandle class. We can use java.lang.invoke.MethodHandles.Lookup static factory method to create Variable Handle objects. It can also be used to access a single element in the array, and byte[] array.Syntaxpublic abstract class VarHandle extends ObjectExampleimport java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import java.util.Arrays; public class VarHandleTest {    public static void main(String args[]) {       VarHandle varHandle = MethodHandles.arrayElementVarHandle(int[].class); ... Read More

What is the importance of the ProcessHandle interface in Java 9?

raja
Updated on 13-Mar-2020 11:27:01

428 Views

ProcessHandle interface introduced in Java 9. It allows us to perform actions and check the state of a process that relates. This interface provides the process’s native process ID (pid), start time, accumulated CPU time, arguments, command, user, parent process, and descendants.ProcessHandle interface allows us to perform the following actions.It returns a ProcessHandle.Info containing further information about a processThe Pid of a processIf it is aliveRetrieve a snapshot of the direct children of a processRetrieve a snapshot of all descents of a processRetrieve a snapshot of all currently running processesAllow the process to be destroyedIt returns a CompletableFuture with a ProcessHandle for when the ... Read More

Advertisements