Raja has Published 760 Articles

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

raja

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 ... Read More

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

raja

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 ... Read More

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

raja

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 ... Read More

How to initialize immutable collections in Java 9?

raja

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 ... Read More

What are the rules for private methods in an interface in Java 9?

raja

raja

Updated on 21-Feb-2020 12:43:46

754 Views

Java 9 added a new feature of private methods to an interface. The private methods can be defined using a private modifier. We can add both private and private static methods in an interface from Java 9 onwards.Rules for private methods in an interface:A private method has a body in an interface means that we can’t be declared ... Read More

What are the new methods added to an Optional class in Java 9?

raja

raja

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

170 Views

An Optional class provides a container that may or may not contain a non-null value. This Optional class introduced in Java 8 to reduce the number of places in the code where a NullPointerException can be generated. Java 9 added three new methods to Optional class: or(), ifPresentOrElse() and stream() that help ... Read More

What are the new features added to Stream API in Java 9?

raja

raja

Updated on 21-Feb-2020 12:20:30

96 Views

In Java 9, Oracle Corporation has added four useful new methods to Stream API. Those methods are iterate(), ofNullable(), takeWhile() and dropWhile().iterate()The iterate() can be used as stream version replacement of traditional for-loops. This method has improved by adding another parameter, the Predicate interface that allows us to stop these endless numbers ... Read More

Can a diamond operator be used with an anonymous inner class in Java 9?

raja

raja

Updated on 21-Feb-2020 12:18:26

102 Views

Yes, we can use the diamond operator with an anonymous inner class since Java 9.The purpose of using a diamond operator is to avoid redundant code and make it more readable by no longer using a generic type on the right side of an expression. The diamond operator used only for normal classes but not for ... Read More

@SafeVarargs annotation for private methods in Java 9?

raja

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(...) {   ... Read More

What are the CompletableFuture API improvements in Java 9?

raja

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", ... Read More

Advertisements