Found 2616 Articles for Java

What are the changes in Memory Management in Java 9?

raja
Updated on 03-Apr-2020 14:09:51

221 Views

Garbage Collection or simply GC is the core part of Memory Management in Java. It can be responsible for cleaning dead objects from memory and reclaiming that space. GC executes cleanup using predefined Garbage Collectors that uses certain algorithms.There are a few important types of Garbage Collectors listed belowSerial GC: A single thread collector and applies to small applications with small data usage. It can be enabled by specifying the command-line option: -XX:+UseSerialGC.Parallel GC: Parallel GC uses multiple threads to perform the garbage collecting process, and it's also known as the throughput collector. It can be enabled by explicitly specifying the option: -XX:+UseParallelGC.G1 Garbage First:  G1 (Garbage ... Read More

How can we import a gson library in JShell in Java 9?

raja
Updated on 02-Apr-2020 16:46:03

251 Views

Java 9 introduced an interactive REPL command-line tool named JShell. It allows us to execute Java code snippets and get immediate results. We can import external classes that can be accessed from a JShell session through the classpath. The Gson library is a Java serialization/deserialization library intended for converting Java Objects into JSON and vice-versa.In the below code snippet, we can set the classpath in JShelljshell> /env --class-path C:\Users\User\gson.jar | Setting new options and restoring state.Once we have imported the gson library in JShell, able to see that library in the list.jshell> import com.google.gson.* jshell> /import | import java.io.* | import java.math.* | ... Read More

What are the different status states of REPL in Java 9?

raja
Updated on 02-Apr-2020 12:50:55

177 Views

REPL stands for Read-Evaluate-Print-Loop. It holds some states, and each statement in JShell has a state. This state denies the execution status of snippets and variables. It can be determined by the results of the eval() method of JShell instance, which evaluates the code.There are seven different status states listed below.DROPPED: The snippet is inactive.NONEXISTENT: The snippet is inactive because it does not yet exist.OVERWRITTEN: The snippet is inactive because it has been replaced by a new snippet.RECOVERABLE_DEFINED: The snippet is a declaration snippet with potentially recoverable unresolved references or other issues in its body.RECOVERABLE_NOT_DEFINED: The snippet is a declaration snippet with ... Read More

How to implement the encapsulation concept in JShell in Java 9?

raja
Updated on 02-Apr-2020 09:44:51

72 Views

Java Shell (simply JShell) is a REPL interactive tool for learning the Java and prototyping Java code. It evaluates declarations, statements, and expressions as entered and immediately prints out the result and runs from the command-line.Encapsulation is an important concept in Java to make sure that "sensitive" data has been hidden from users. To achieve this, we must declare a class variable as private and provides public access to get and set methods and update the value of a private variable.In the below code snippet, we have implemented the Encapsulation concept for Employee class.jshell> class Employee { ...>       private String firstName; ...>     ... Read More

How can we create a multi-release jar(mrjar) using jar tool in Java 9?

raja
Updated on 01-Apr-2020 16:29:09

158 Views

In Java 9, a new feature "multi-release jar format" has been introduced where jar format enhanced with different versions of Java class or resources that can be maintained and used as per the platform. A jar command can be used to create a multi-release jar that contains two versions of the same class compiled for both Java 8 and Java 9 versions with a warning message, telling that both classes are identical.C:\Users\User\tutorialspoint>jar --create --file MR.jar -C sampleproject-base demo --release 9 -C sampleproject-9 demo Warning: entry META-INF/versions/9/demo/SampleClass.class contains a class thatis identical to an entry already in the jarThe " --release 9" option can tell jar ... Read More

How to check a string is palindrome or not in Jshell in Java 9?

raja
Updated on 01-Apr-2020 15:47:43

110 Views

JShell is the first REPL(Read-Evaluate-Print-Loop) interactive tool that has been introduced as a part of Java 9. It evaluates declarations, statements, and expressions as entered and immediately shows the results, and it runs from the command-line prompt.Palindrome string is a string where it remains the same when reversed or word spelled the same way in both forward and backward directions.In the below example, we can able to check whether the given string is palindrome or not in the JShell tool.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> String str="LEVEL"; str ==> "LEVEL" jshell> ... Read More

What are the core library changes in Process API in Java 9?

raja
Updated on 01-Apr-2020 10:01:49

88 Views

In Java 9, one can retrieve the PID of the process through a native call and can be achievable through the ProcessHandle. We can also retrieve information about the currently running Java Process (JVM) and Info (inner class of ProcessHandle) class that contains details about the process. We can also return a snapshot of all currently running processes in the system.Exampleimport java.lang.ProcessHandle.Info; public class ProcessAPIChanges {    public void detailedAPIInfo(ProcessHandle processHandle) {       Info processInfo = processHandle.info();       System.out.println("Detailed Process Info is Provided Below: ");       System.out.println("[Executable Name] " + processInfo.command().get());       System.out.println("[User Name] " + ... Read More

Which modifiers can't allow in the top-level declaration in JShell in Java 9?

raja
Updated on 01-Apr-2020 07:52:42

122 Views

JShell is an interactive tool for learning the Java language and prototyping Java code. It is a REPL (Read-Evaluate-Print-Loop) that evaluates declarations, statements, and expressions once entered and immediately prints the results in JShell. This tool runs from the command-line prompt.The modifiers like public, protected, private, static, and final have not allowed on top-level declarations and can be ignored with a warning. The keywords like synchronized, native, abstract, and default top-level methods have not allowed and can be errors.In the below code snippets, we have created both final and static variables. It prints out a warning message to the user that "Modifier 'final' or 'static' not permitted ... Read More

Importance of MethodHandles class in Java 9?

raja
Updated on 31-Mar-2020 15:48:15

167 Views

MethodHandles class introduced in Java 7 version. This class primarily added some static methods to better the functionality, and falls into several categories like Lookup methods that help to create method handles for methods and fields, Combinator methods that combine or transform pre-existing method handles into new ones, and factory methods to create method handles that emulate other common JVM operations or control flow patterns. MethodHandles class has enhanced in Java 9 to introduce many changes and added new static methods like arrayLength(), arrayConstructor(), zero(), and etc.Syntaxpublic class MethodHandles extends ObjectExampleimport java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; public class MethodHandlesTest {    public void MethodHandle1() {   ... Read More

How to print all attributes in StackFrame API in Java 9?

raja
Updated on 31-Mar-2020 12:41:29

130 Views

StackWalker API is a new feature in Java 9, and it improves the performance of the predecessor stack track element. It can also provide a way to filter the stack elements in case of exception or to understand application behavior. In Java 9, the way to access the stack trace is very limited and provide the entire stack information at once.In the below example, we need to print all attributes in Stack Frame Exampleimport java.lang.StackWalker.StackFrame; import java.util.*; import java.util.stream.*; import java.lang.StackWalker.Option; public class AllAttributesTest {    public static void main(String args[]) {       System.out.println("Java 9 Stack Walker API - Print all ... Read More

Advertisements