Programming Articles - Page 2089 of 3366

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

Alshifa Hasnain
Updated on 10-Jun-2025 18:41:50

1K+ 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. Below, we can define expressions, variables, and methods in JShell. Expression The combination of variables, operators, and methods is known as an Expression in Java. They perform some logic that evaluates to a single value. int a=5, b=10, c=0; c=a+b; Here, "a+b" is an expression that evaluates the value of c as 15. Expression in JShell We can type any valid Java expression in JShell. The expression ... Read More

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

Alshifa Hasnain
Updated on 10-Jun-2025 18:40:01

526 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 the need to compile the code before executing it. JShell is easily started by typing "jshell" in the command prompt. What are Snippets? JShell allows the execution of Java statements,  variables, methods, class definitions, imports, and expressions. These pieces of Java code are referred to as snippets. Every snippet is assigned an ID(unique) that we can use later to reference it. Example Below is an example to represent a snippet ... Read More

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

Alshifa Hasnain
Updated on 14-Jul-2025 18:40:39

216 Views

In this article, we will learn about the new methods added to the Process API in Java 9. First, we will know about what is process API, and the methods in Process API in detail. What is Process API? In Java, the Process API, we can perform any operation regarding a process. If one must get the process ID of currently running process, or to create a new process, or destroy an already running one, or find child and parent processes of currently running process, or just get any information about a process, then the Process API is used to perform ... Read More

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

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

599 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

442 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

447 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

Differences between orTimeout() and completeOnTimeOut() methods in Java 9?

Alshifa Hasnain
Updated on 09-Jun-2025 14:27:47

2K+ Views

In this article, we will learn about the differences between the orTimeout() and the completeOnTimeout() methods in Java. Both the orTimeout() and completeOnTimeout() methods are defined in the CompletableFuture class, and these two methods were introduced in Java 9. CompletableFuture class The CompletableFuture class, introduced in Java 8, represents a Future that can have its value and status set explicitly. It also supports dependent functions and actions that are triggered upon the completion of the future. In Java 9, the CompletableFuture API received further enhancements. The following are some common methods of the CompletableFuture class: ... Read More

When to use the ofNullable() method of Stream in Java 9?

Alshifa Hasnain
Updated on 10-Jun-2025 18:39:17

2K+ Views

In this article, we will learn to use the ofNullable() method of Stream in Java 9. We will first go through the Stream class, after that we will learn about the ofNullable() method along with its syntax and real-world use cases. Stream Class Stream was introduced in Java 8, the Stream API is used to process collections of objects. It represents a sequence of elements that support various operations to perform computations in parallel. Creating an instance of a stream: Stream stream; A stream is not a type of data structure, it takes input from Collections, Arrays, etc. The ofNullable() ... Read More

How can we create an unmodifiable Map in Java 9?

Alshifa Hasnain
Updated on 09-Jun-2025 14:28:11

454 Views

In this article, we will learn to create an unmodifiable Map in Java 9. First, we will learn about Map and Unmodifiable Map and after that we will be knowing the different ways through which we can create an unmodifiable map (Map.of method and unmodifiableMap() Method). Map In Java, a Map is an interface in the Collections framework used to store data in the form of key-value pairs. Each key in the map is unique and maps to some value. Some of the implementations of the Map interface are: HashMap ... Read More

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

Alshifa Hasnain
Updated on 10-Jun-2025 18:42:26

267 Views

In this article, we will learn about the new methods that have been added to the Arrays class in Java 9. First, we will learn about arrays, and then the new methods added to the equals(), compare(), and mismatch() methods with examples. Arrays Java provides a data structure called the Array, and they are used to store multiple values of the same datatype in a single variable. The Arrays class can contain various methods for manipulating arrays and also contains static factory methods that allow arrays to view as a list. Creating an instance of an Array with a variable ... Read More

Advertisements