Found 9150 Articles for Object Oriented Programming

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

Alshifa Hasnain
Updated on 11-Jun-2025 13:41:57

3K+ Views

In this article, we will learn about the purpose of using the Optional.ifPresentOrElse() method in Java 9.  The Optional Class The Optional class was introduced in Java 8. Many Java developers face null pointer exceptions to avoid this, we need to put null checks we need to put if, to check if a method is null we can put it in the if statement; otherwise, we have to put it in the else statement. And to check this, we need to put this at multiple points to avoid this situation, We use the Optional class. It avoids the null pointer ... Read More

How to import external libraries in JShell in Java 9?

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

2K+ Views

In this article, we will learn to import external libraries in JShell in Java 9. JShell is an interactive tool to learn the Java language and prototype Java code. This tool works on the principle of REPL (Read-Evaluate-Print-Loop). Default Imports in JShell 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 ... Read More

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

Alshifa Hasnain
Updated on 11-Jun-2025 13:40:07

354 Views

Since Java 9, we are 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 need to share some code, a private method can be created for the same and called from each of the default methods. Different Types of Variables and Methods in an Interface In Java 9, the following variables/methods have been defined in an interface. Constant Variables Abstract Method ... Read More

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

Alshifa Hasnain
Updated on 11-Jun-2025 17:28:34

2K+ Views

In this article, we will learn about the use of the Optional.stream() method in Java 9. First, we will learn about the optional class and its method. Then we will see the use cases along with an example.Optional class The Optional class was introduced in Java 8 to reduce the null pointer exception that occurs in Java. It is basically a container for the actual object that needs to be returned by a method. The point here is that if a method returns a null value, which can be null, then that method could return a value instead contained inside ... Read More

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

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

1K+ Views

In this article, we will learn about the use of the Cleaner class in Java 9. Below, we will first tell the basic usage and then we will know how we can use the Cleaner method practically. Garbage Collector An Object that has been created during the program execution is automatically removed by the 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 finalize() method The Object class has a finalize() method, which is automatically called by GC before it attempts to remove the ... Read More

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

504 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

195 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

571 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

431 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

Advertisements