Found 9150 Articles for Object Oriented Programming

JShell in Java 9?

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

232 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

653 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

304 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

366 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

2K+ 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

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

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

176 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 anonymous inner classes in Java 7. If we try to use it for anonymous inner classes, the compiler throws an error.In the below example, we have used a diamond operator with an anonymous inner class.Exampleimport java.util.*; public class DiamondOperatorTest {    public static void main(String args[]) {       String[] str = ... Read More

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

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

185 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 based on conditions defined with the Predicate interface.Exampleimport java.util.stream.Stream; public class StreamIterateMethodTest {    public static void main(String[] args) {       Stream.iterate(1, i -> i < 5, i -> i + 1).forEach(System.out::println); // iterate()    } }Output1 2 3 4ofNullable()The ofNullable() method returns the stream object of an element if it ... Read More

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

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

266 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 us to deal with default values.Optional.or()The or() method introduced in Java 9 and the parameter of this method is a functional interface Supplier. This method always gives us an Optional object that is not empty. If the Optional object is not empty, it returns the Optional object itself. Otherwise, it returns an Optional ... Read More

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

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

1K+ 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 as a normal abstract method as usually do in an interface. If we are trying to declare a private method without a body then it can throw an error says that "This method requires a body instead of a semicolon".We can't be used both private and abstract modifiers together in an interface.If ... Read More

What are the main features and enhancements introduced in Java 9?

raja
Updated on 11-Feb-2020 08:00:28

245 Views

Oracle has released Java 9 version with a rich set of new features and brings a lot of new enhancements.Below are a few important features and enhancements introduced in Java 9.Factory Methods for Collections: Factory methods are special kinds of static methods that can be used to create unmodifiable instances of collections, which means we can use these methods to create a list, set, and map.Java Platform Module System (JPMS): A Java Module is a mechanism to bundle java applications and java packages into a Java module. It specifies which of the java packages that contain visible to other java modules by using ... Read More

Advertisements