Found 2616 Articles for Java

How to initialize immutable collections in Java 9?

raja
Updated on 21-Feb-2020 13:00:50

1K+ Views

Java 9 provides factory methods to create immutable lists, sets, and maps. It can be useful to create empty or non-empty collection objects. In Java 8 and earlier versions, we can use collection class utility methods like unmodifiableXXX to create immutable collection objects. If we need to create an immutable list then use the Collections.unmodifiableList() method.These factory methods allow us for easy initialization of immutable collections whether they are empty or non-empty.Initialization of immutable list:List immutableEmptyList = List.of();In the above, we have initialized an empty, immutable List.Initialization of immutable set:Set immutableEmptySet = Set.of();In the above, we have initialized an empty, immutable Set.Initialization of immutable map:Map immutableEmptyMap = Map.of();In the ... Read More

What are the useful commands in JShell in Java 9?

raja
Updated on 24-Feb-2020 06:03:18

366 Views

Java 9 has introduced a new interactive tool called JShell. This tool can be used to execute, test user-friendly and easy way of java classes, interfaces, enums, objects, statements and etc. JShell can do the work by evaluating the commands that user types into it. It works on the principle of REPL(Read-Evaluate-Print-Loop).Below are some of the important commands in JShell/var − This command can be used to get a list of all variables used. While performing the calculations, JShell creates implicit variables. As soon as we type the /var command, it displays all variables declared so far. For instance $1, $2 and $3 ... Read More

How to create a class and object in JShell in Java 9?

raja
Updated on 17-Feb-2020 13:36:48

246 Views

JShell is a new java shell tool released in java 9. It is the first official REPL (Read-Evaluate-Print-Loop) application. This tool helps in executing and evaluating simple java programs and logics such as statements, loops, expressions, and etc. Java REPL provides a simple programming environment in the command prompt. It can read the input, evaluate it and print the output.In the below example we can able to create a class and object in JShell using command prompt.Examplejshell> class Employee { ...> private String name; ...>    Employee(String name) { ...>       this.name=name; ...> } ...> ... Read More

StackWalker API in Java 9?

raja
Updated on 24-Feb-2020 06:05:26

577 Views

StackWalker API allows easy filtering and lazy access to execute tasks within any method. It is an efficient API for obtaining stack trace information in Java 9.There are three new important classes in StackWalker API: StackWalker,  StackWalker.StackFrame and StackWalker.Option.StackWalker − It is the main class in StackWalker API. We traverse stack frames by using StackWalker.forEach() method and get caller class in an efficient way by calling StackWalker.getCallerClass() method. We walk through stack traces and applying a function on a stream of stack frames by using StackWalker.walk() method.StackWalker.StackFrame − It is a static nested class of StackWalker and represents method invocation return by StackWalker. It has methods ... Read More

Differences between takewhile() and dropWhile() methods in Java 9?

raja
Updated on 21-Feb-2020 13:05:17

2K+ Views

The takewhile() method of Stream API accepts all values until predicate returns false whereas dropWhile() method of Stream API drops all values until it matches the predicate. If a stream is ordered, the takewhile() method returns a stream consisting of the longest prefix of elements taken from this stream that matches predicate whereas the dropWhile() method returns the remaining stream after matching predicate. If the stream is unordered, the takewhile() method returns a stream consisting of a subset of elements extracted from a stream that matches the given predicate whereas the dropWhile() method returns a stream consisting of the remaining elements of a stream after dropping ... Read More

JShell in Java 9?

raja
Updated on 17-Feb-2020 05:05:11

134 Views

JShell is a new concept introduced in Java 9 version. It provides Java with REPL (Read-Eval-Print-Loop) ability. By using JShell, we can test the java-based logic and expressions without compiling it. REPL acts as an immediate feedback loop and has a great effect on productivity in that particular language.Step 1: Open Command Prompt and type JShell.Microsoft Windows [Version 6.3.9600] (c) 2013 Microsoft Corporation. All rights reserved. C:\Users\User>JShell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell>Step 2: Type /help (to view JShell commands) in the JShell command window once it starts running.jshell> /help | Type ... Read More

Which attributes have added to @Deprecated annotation in Java 9?

raja
Updated on 21-Feb-2020 12:14:47

529 Views

There are two new parameters or attributes added to @Deprecated annotation in Java 9. Those parameters are Since and forRemoval, both of these two parameters are optional with a default value when we can't specify it.SinceThis string parameter specifies the version in which the API became deprecated. The default value of this element is an empty string.Syntax@Deprecated(since="")forRemovalThis boolean parameter specifies whether the API is intended to be removed in a future release or not. The default value is false when we can't specify it.Syntax@Deprecated(forRemoval=)Examplepublic class DeprecatedAnnotationTest {    public static void main(String[] args) {       DeprecatedAnnotationTest test = new DeprecatedAnnotationTest();       test.method1();   ... Read More

What are the CompletableFuture API improvements in Java 9?

raja
Updated on 21-Feb-2020 12:15:49

220 Views

CompletableFuture API is used for asynchronous programming in Java. It means that we can write non-blocking code by running a task on a separate thread than the main() thread and notify the main() thread about its progress, completion or failure. Java 9 introduces a few improvements in CompletableFuture API,  they are: "Support for timeouts and delays", "Improved support for subclassing" and "Addition of new factory methods".Support for timeouts and delayspublic CompletableFuture orTimeout(long timeout, TimeUnit unit) The above method has been used to specify if the task does not complete within a certain period of time the program stops and throws TimeoutException.public CompletableFuture completeOnTimeout(T value, long timeout, ... Read More

@SafeVarargs annotation for private methods in Java 9?

raja
Updated on 21-Feb-2020 12:16:34

244 Views

The @SafeVarargs annotation was introduced in Java 7. This annotation applies to both final and static methods or constructors that take varargs parameters. This annotation used to make sure that a method does not perform unsafe operations on its varargs parameters. Since Java 9, @SafeVarargs annotation also applies to private instance methods.Syntax@SafeVarargs private void methodName(...) {    // some statements }Exampleimport java.util.ArrayList; import java.util.List; public class SafevarargsTest {    @SafeVarargs     // Apply @SafeVarargs to private methods    private void display(List... names) {       for(List name : names) {          System.out.println(name);       }    }    public static void ... Read More

What is the use of underscore keyword in Java 9?

raja
Updated on 13-Feb-2020 10:29:35

1K+ Views

In earlier versions of Java, the underscore ("_") has used as an identifier or to create a variable name. Since Java 9, the underscore character is a reserved keyword and can't be used as an identifier or variable name. If we use a single underscore character as an identifier, the program fails to compile and throws a compile-time error because now it is a keyword and can't be used as a variable name in Java 9 or later versions.Examplepublic class UnderscoreKeywordTest {    public static void main(String args[]) {       int _ = 50       System.out.println(_);   ... Read More

Advertisements