Java Articles

Page 323 of 450

How to implement java.time.LocalDate using JShell in Java 9?

raja
raja
Updated on 22-Apr-2020 780 Views

JShell is a REPL (Read-Eval-Print-Loop) interactive tool introduced in Java 9 that takes input, evaluates it, and returns output to a user.java.util.LocalDate class provides a number of methods to retrieve Date information: Day/Month/Year and related attributes Date meta-information: Classification-related information such as whether a leap year, etc. LocalDate class is immutable, and we can use different methods provided to add and subtract days, months, and years. Each of these returns a new instance of LocalDate.In the below two code snippets, we can able to print different operations using LocalDate class.Snippet-1jshell> import java.time.*; jshell> LocalDate today = LocalDate.now() today ==> ...

Read More

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

raja
raja
Updated on 22-Apr-2020 817 Views

A Publisher is a provider of an unbounded number of sequenced elements publishing them according to demand received from its Subscribers. Publisher interface is responsible for publishing elements of type T and provides a subscribe() method for subscribers to connect to it.public interface Publisher {    public void subscribe(Subscriber

Read More

How to implement relational and logical operators in JShell in Java 9?

raja
raja
Updated on 22-Apr-2020 242 Views

JShell has introduced in Java 9 that enables us to explore, discover, and experiment with Java language features, and extensive libraries.The relational operators (==, != , =) can be used mainly for comparison. It accepts operands of non-boolean primitive data types and returns a boolean value. JShell also supports logical operators that can be used in expressions. The logical operators can expect boolean operands. The expressions involving these operands can be used for forming boolean conditions in the code within if, for, and while statements. The logical operators include : "&& : logical AND", "|| : OR" and "! : ...

Read More

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

raja
raja
Updated on 22-Apr-2020 401 Views

A Subscription can be shared by exactly one Publisher and one Subscriber for the purpose of mediating data exchange. That is the reason subscribe() method doesn't return created Subscription, instead returns void. The Subscription is only passed to Subscriber through the onSubscribe() method callback. The Subscription interface contains two methods: request() and cancel().Syntaxpublic interface Subscription { public void request(long n); public void cancel(); }Rules for Subscription interface:Subscription.request() and Subscription.cancel() methods must be called only inside of its Subscriber context.Subscription must allow Subscriber to call the Subscription.request() method synchronously from within onNext() or onSubscribe() methods.Subscription.request() ...

Read More

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

raja
raja
Updated on 21-Apr-2020 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 ...

Read More

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

raja
raja
Updated on 21-Apr-2020 304 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, ...

Read More

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

raja
raja
Updated on 21-Apr-2020 522 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
raja
Updated on 20-Apr-2020 451 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
raja
Updated on 20-Apr-2020 304 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 ...

Read More

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

raja
raja
Updated on 17-Apr-2020 929 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

Read More
Showing 3221–3230 of 4,498 articles
« Prev 1 321 322 323 324 325 450 Next »
Advertisements