Found 2616 Articles for Java

Differences between Optional.ifPresentOrElse() and Optional.or() methods in Java 9?

raja
Updated on 25-Feb-2020 12:42:26

3K+ Views

Both Optional.ifPresentOrElse() and Optional.or() methods have introduced in Java 9 version to improve its functionality. The Optional.ifPresentOrElse() method checks if the value is present, apply action with value, else return empty action whereas Optional.or() method checks if the value is present, return option contains value, else return Optional applies to Supplier funciton. The Optional.ifPresentOrElse() method contains two parameters, Consumer and Runnable whereas Optional.or() method contains only one parameter, Supplier.Syntax of Optional.ifPresentOrElse():public void ifPresentOrElse(Consumer

How to handle an exception in JShell in Java 9?

raja
Updated on 25-Feb-2020 10:13:32

266 Views

In Java 9, JShell provides a fast and friendly environment that enables us to quickly explore, discover, and experiment with Java language features and extensive libraries.In JShell, manually catching the exceptions is not required. JShell automatically catches each exception and displays information about it, then displays the next JShell prompt so we can continue our session. It works for unchecked exceptions also. By automatically catching both checked and unchecked exceptions, JShell makes it easier for us to experiment with methods that throw checked exceptions.In the below example, ArrayIndexOutOfBoundsException has occurred because the value of "values[4]"  not found.Example-1jshell> int[] values = {10, 20, 30} values ==> int[3] { ... Read More

What is the purpose of using Optional.ifPresentOrElse() method in Java 9?

raja
Updated on 25-Feb-2020 07:45:01

2K+ Views

The improvement of ifPresentOrElse() method in Optional class is that accepts two parameters, Consumer and Runnable. The purpose of of using ifPresentOrElse() method is that if an Optional contains a value, the function action is called on the contained value, i.e. action.accept (value), which is consistent with ifPresent() method. The difference from the ifPresent() method is that ifPresentOrElse() has a second parameter, emptyAction. If Optional contains no value, then ifPresentOrElse() method calls emptyAction, i.e. emptyAction.run().Syntaxpublic void ifPresentOrElse(Consumer

How to import external libraries in JShell in Java 9?

raja
Updated on 24-Feb-2020 12:06:57

2K+ Views

JShell is an interactive tool to learn Java language and prototyping Java code. JShell does the work by evaluating the commands that user types into it. This tool works on the principle of REPL (Read-Evaluate-Print-Loop).By default, JShell automatically imports a few useful java packages when the JShell session is started. We can type the command /imports to get a list of all these imports.jshell> /imports | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* | import java.util.* | import java.util.concurrent.* | import java.util.function.* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.* | import javax.mail.internet.InternetAddressWe can also import external libraries in ... Read More

What kind of variables/methods defined in an interface in Java 9?

raja
Updated on 24-Feb-2020 13:11:34

275 Views

Since Java 9, we able to add private methods and private static methods in an interface. The advantage of using private methods in an interface is to reduce code duplication among default and static methods. For instance, if two or more default methods needed to share some code, a private method can be created for the same and called from each of the default methods.In Java 9, the following variables/methods have defined in an interface.ConstantAbstract methodDefault methodStatic methodPrivate methodPrivate Static methodExampleimport java.util.*; import java.util.stream.*; interface InterfaceTest {    static void printEvenNumbers() {       getDataStream().filter(i -> i%2==0).forEach(System.out::println);    }    static void printLOddNumbers() {       getDataStream().filter(i ... Read More

What is the use of the Optional.stream() method in Java 9?

raja
Updated on 24-Feb-2020 05:30:32

1K+ Views

In Java 9, the stream() method has been added to the Optional class to improve its functionality. The stream() method can be used to transform a Stream of optional elements to a Stream of present value elements. If the Optional contains a value, then return a Stream containing the value. Otherwise, it returns an empty stream.Syntaxpublic Stream stream()Exampleimport java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamMethodTest {    public static void main(String[] args) {       List list = Arrays.asList(             Optional.empty(),             Optional.of("TutorialsPoint"),           ... Read More

What is the use of the Cleaner class in Java 9?

raja
Updated on 24-Feb-2020 05:31:36

734 Views

An Object that has been created during the program execution is automatically removed by Garbage Collector (GC). When an object not referenced by any thread and when JVM determines that this object can't be accessed, then it can be eligible for garbage collection.The Object class has a finalize() method, which is automatically called by GC before it attempts to remove the object from the heap. In Java 9, the finalize() method has been deprecated and a new class java.lang.ref.Cleaner added to garbage collection management. An object of Cleaner class gets notified automatically when an object becomes eligible for garbage collection. An object that is being ... Read More

How to define expressions, variables, and methods in JShell in Java 9?

raja
Updated on 24-Feb-2020 05:32:55

706 Views

JShell is a Read-Evaluate-Print Loop (REPL) that evaluates declarations, statements, and expressions as we have entered and immediately shows the results. This tool is run from the command prompt.In the below, we can define expressions, variables, and methods in JShell.ExpressionWe can type any valid Java expression in JShell. The expression is either an arithmetic operation, string manipulation, and method call and evaluates immediately. All the results automatically assigned to a variable created by JShell. These variables have prefixed with $ symbol.Examplejshell> 10 * 5 $1 ==> 50 jshell> 77 % 3 $2 ==> 2 jshell> $1 + $2 $3 ==> 52 jshell>VariableThe Variables defined in ... Read More

What is the use of the Tab key in JShell in Java 9?

raja
Updated on 21-Feb-2020 10:22:01

202 Views

JShell can also provide an auto-completion feature when we partially type the name of an existing class, variable, or method by pressing the Tab key. If an item can't determine from what we entered, then possible options are provided.Pressing the Tab key in JShell performs one of the following tasks:If no other name matches what we have typed, JShell enters the rest of the name for us.If there are multiple names that begin with the same letters, then JShell displays a list of those names to help what to type next, then type the next letter(s) and press tab key again to complete the name.If no ... Read More

How to save, edit, and drop a snippet in JShell in Java 9?

raja
Updated on 21-Feb-2020 10:06:42

335 Views

Java Shell or JShell is an official REPL(Read-Evaluate-Print-Loop) introduced with Java 9. It provides an interactive shell for quickly prototyping, debugging and without the need for the main() method or without the need to compile the code before executing it. JShell is easily started by typing "jshell" in the command prompt. Save a snippetWe can save a snippet source to a file by using "/save [-all|-history|-start] " command.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> /save C:\Users\User\jshell.txtThe above code creates a new "jshell.txt under the specified path.Edit a snippetWe can also edit the code in an ... Read More

Advertisements