Found 2612 Articles for Java

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

735 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

709 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

What are the new methods added to Process API in Java 9?

raja
Updated on 21-Feb-2020 04:57:55

95 Views

Java 9 improves Process class by adding new methods and also provides a new interface: ProcessHandle and ProcessHandle.Info to get all the details about the process and its information.Below is the list of new methods added to Process in Java 9boolean supportsNormalTermination(): It can return true if the implementation of the destroy() is to normally terminate the process, else returns false.long pid(): It can return the native process ID of the process.ProcessHandle toHandle(): It can return a ProcessHandle for the Process.Stream children(): It can return a snapshot of the direct children of the process.Stream descendants(): It can return a snapshot of the descendants ... Read More

What are the advantages of private methods in an interface in Java 9?

raja
Updated on 21-Feb-2020 04:59:01

438 Views

In Java 9, an interface can also have private methods. Apart from static and default methods in Java 8, this is another significant change as it allows the re-usability of common code within the interface itself.In an interface, there is a possibility of writing common code on more than one default method that leads to code duplication. The introduction of private methods avoids this code duplication.Advantages of private methods in an interfaceAvoiding code duplication.Ensuring code re-usability.Improving code readability.Syntaxinterface interfacename {    private methodName(parameters) {       // statements     } }Exampleinterface Test {    default void m1() {       common();    }   ... Read More

Importance of iterate() method of Stream API in Java 9?

raja
Updated on 21-Feb-2020 05:11:39

356 Views

In Java 8, the iterate() method of Stream API takes the seed and the unary operator as arguments. As stream becomes infinite, it makes the developer add explicit termination conditions by using limit, findFirst, findAny and etc. In Java 9, the iterate() method of Stream API has added a new argument, a predicate that takes the condition to break the flow.Syntaxstatic Stream iterate(T seed, Predicate

What are the benefits of immutable collections in Java 9?

raja
Updated on 21-Feb-2020 10:08:12

341 Views

In Java 9, several factory methods have added to Collections API. By using these factory methods, we can create unmodifiable list, set and map collection objects to reduce the number of lines of code. The List.of(), Set.of(), Map.of() and Map.ofEntries() are the static factory methods that provide convenient way of creating immutable collections in Java 9.Benefits of Immutable CollectionsLess heap space: The space required to store a collection data is very less as compared with the traditional approach in earlier versions of java.Faster access to data: As the overhead to store data and wrap into Collections.unmodifiable is reduced, now data access becomes faster. It means that ... Read More

Advertisements