Found 2616 Articles for Java

What are the improvements for @Deprecated annotation in Java 9?

raja
Updated on 21-Apr-2020 18:23:10

84 Views

Any element that can be annotated with @Deprecated signifies that this particular element no longer be used for below reasonsUsing it is risky and may cause errors.May be incompatible in future versions.May be removed in future versions.A better and more efficient solution has replaced it.Java 9 has added two new elements: since and forRemoval attributes.1) since: The element specifies the deprecated version of the annotated API element.2) forRemoval: The element representing the annotated API element can be removed in a future version, and the API can be migrated.The following webpage is the documentation for a Boolean class in Java 9. The @Deprecated annotation ... Read More

How to declare multiple resources in a try-with-resources statement in Java 9?

raja
Updated on 21-Apr-2020 14:46:32

1K+ Views

Try-with-resources statement has been improved in Java 9. If we already have a resource that is final or equivalent to the final variable, then we can use that variable in a try-with-resources statement without having to declare a new variable in a try-with-resources statement.We can declare multiple resources in a try block. Try initialization block can have any number of resources resulting in either null or non-null resources.In the below example, we can able to declare multiple resources in the try-with-resources statement.Exampleimport java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; public class MultipleResourcesTest {    public static void main(String args[]) throws ... Read More

How to implement integer type conversion in JShell in Java 9?

raja
Updated on 21-Apr-2020 12:03:51

179 Views

JShell is a command-line interactive tool introduced in Java 9 version that allows the programmer to execute simple statements, expressions, variables, methods, classes, interfaces, etc.. without declaring the main() method.In JShell, the compiler warns the programmer about typecasting issues by throwing errors. However, if the programmer is aware of it, then explicit casting will be required. If we need to store a smaller data value into a larger type conversion, then implicit casting will be required.There are two kinds of integer typecasting:Literal-to-variable assignment: For instance, short s1 = 123456, the data is out of range. It is known at compile-time, and the compiler flags an ... Read More

What are the rules for the Subscriber interface in Java 9?

raja
Updated on 21-Apr-2020 08:38:18

294 Views

Subscriber interface subscribes to publishers to receive items through onNext() method, error message through the onError() method, or a signal that no more items to be expected through the onComplete() method. Before any of those things happen, the publisher calls onSubscription() method.public interface Subscriber {    public void onSubscribe(Subscription s);    public void onNext(T t);    public void onError(Throwable t);    public void onComplete(); }Rules for Subscriber interface:A Subscriber must call through Subscription.request(long n) method to receive onNext() signals.Subscriber.onComplete() and Subscriber.onError(Throwable t) methods must not call any methods on Subscription or Publisher.Subscriber.onComplete() and Subscriber.onError(Throwable t) methods must consider the Subscription canceled after received ... Read More

How to create a process using ProcessBuilder in Java 9?

raja
Updated on 20-Apr-2020 16:24:53

301 Views

Java 9 added ProcessHandle interface to Process API to enhance Process class. An instance of the ProcessHandle interface identifies a local process that allows us to query process status and managing processes, and ProcessHandle.Info allows us to use local code because of the need to obtain the PID of a local process.ProcessBuilder class can be used to create separate operating system processes. In the below example, we can create a process of "notepad" application by using the ProcessBuilder class.Exampleimport java.time.ZoneId; import java.util.stream.Stream; import java.util.stream.Collectors; import java.io.IOException; public class ProcessBuilderTest {    public static void main(String args[]) throws IOException {       ProcessBuilder pb = new ProcessBuilder("notepad.exe"); ... Read More

How to create Html5 compliant Javadoc in Java 9?

raja
Updated on 20-Apr-2020 10:20:55

183 Views

Before Java 9, we have to search in google to find out particular packages, class, interface, and method information. Since Java 9, Javadoc includes search options in the API documentation itself, and the output is HTML5 compliant.In the below example, we have created the "JavaDocTest.java" file in the "C:/JAVA" folder.Examplepublic class JavaDocTest {    /**       * Default method to be run to print       * Tutorialspoint       * @param args command-line arguments    */    public static void main(String args[]) {       System.out.println("Tutorialspoint");    } }The documentation generated by Java 9 ... Read More

Importance of Optional.or() method in Java 9?

raja
Updated on 17-Apr-2020 17:40:48

596 Views

In Java 9,  few static methods: stream(), or(), and ifPresentOrElse() have added to Optional class. The introduction of an Optional class solves the null pointer exception.Optional.or() method returns an Optional describing the value if a value is present, otherwise returns an Optional produced by the supplying function. Syntaxpublic Optional or(Supplier

How to create static VarHandle in Java 9?

raja
Updated on 17-Apr-2020 09:28:06

375 Views

VarHandle is a reference to a variable, and it provides access to variables under various access modes (such as plain read/write, volatile read/write, and compare-and-swap), similar to the functionality provided by java.util.concurrent.atomic and sun.misc.Unsafe. The variables can be array elements, instance or static fields in a class.In the below example, we can create a static variable handle.Exampleimport java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; public class StaticVarHandleTest { static int field; static int[] array = new int[20]; static final VarHandle FIELD, ARRAY; static { try { ... Read More

What are the rules for external declarations in JShell in Java 9?

raja
Updated on 16-Apr-2020 19:10:32

79 Views

JShell is a command-line tool introduced in Java 9, and it is Java's first official REPL tool to create a simple programming environment that reads the user's inputs, evaluates it, and prints the result.The declarations outside a class or interface (and declarations of classes and interfaces by themselves) have been created under the following rules.Rules for External Declarations:1) Access modifiers like public, protected, and private can be ignored. All declaration snippets can be accessible to all other snippets.jshell> private int i = 10; i ==> 10 jshell> System.out.println(i); 102) The modifier final can be ignored. Changes and inheritance are allowed.jshell> final class A {void m() {} } ... Read More

How can we create an instance of VarHandle in Java 9?

raja
Updated on 16-Apr-2020 14:11:28

141 Views

In general, a Variable Handle is simply typed reference to a variable. It will be an array element, an instance or static field of the class. VarHandle class can provide write and read access to variables under specific conditions. These are immutable and have no visible condition. In addition, they can't be sub-classified, and each VarHandle has a generic type T, which is the type of each variable represented by this VarHandle. The objective of VarHandle is to define a standard for calling equivalents of java.util.concurrent.atomic and sun.misc.Unsafe operations on fields and array elements.In the below example, we can use ... Read More

Advertisements