Found 2616 Articles for Java

How to get dates using LocalDate.datesUntil() method in Java 9?

raja
Updated on 28-Feb-2020 11:37:48

1K+ Views

The LocalDate.datesUntil() method creates a stream between two local date instances and allows us to optionally specify a step size. This method has two variations, the first one takes end date and gives a list of dates between the current date and end date whereas the second one takes a Period object as a parameter that provides a way to skip dates and stream only a select subset of the dates between start and end dates.Syntaxpublic Stream datesUntil(LocalDate end) public Stream datesUntil(LocalDate end, Period step)Exampleimport java.time.LocalDate; import java.time.Period; import java.time.Month; import java.util.stream.Stream; public class DatesUntilMethodTest {    public static void main(String args[]) {       ... Read More

How to get the date and time in JShell in Java 9?

raja
Updated on 28-Feb-2020 08:57:04

362 Views

JShell is an interactive command-line tool that can allow us to learn, investigate, and explore the Java language and their API. We can type any valid java code into the console and get immediate results without the need to write a verbose class with the main() method.If we want to get the current date with time in JShell by using the below code snippet.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> new Date() $1 ==> Fri Feb 28 11:59:23 IST 2020 jshell>In the below code snippet, we need to get a date ... Read More

How to use the collect() method in Stream API in Java 9?

raja
Updated on 27-Feb-2020 13:40:16

1K+ Views

The collect() method in Stream API collects all objects from a stream object and stored in the type of collection. The user has to provide what type of collection the results can be stored. We specify the collection type using the Collectors Enum. There are different types and different operations can be present in the Collectors Enum, but most of the time we can use Collectors.toList(), Collectors.toSet(), and Collectors.toMap().Syntax R collect(Collector

How to create scratch variables in JShell in Java 9?

raja
Updated on 27-Feb-2020 12:18:55

256 Views

JShell is a REPL interactive tool introduced in Java 9 to execute and evaluate simple java programs like variable declarations, statements, expressions, and the programs without using the main() method.In JShell, any value returned by a snippet automatically saved into a scratch variable. These scratch variables can be represented by $. When we do not assign the result of an expression to a variable, a scratch variable is created in JShell so that an output of expression can be used later.In the below code snippet, six scratch variables have createdC:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> ... Read More

How to load a file into the JShell session in Java 9?

raja
Updated on 13-Mar-2020 11:54:32

1K+ Views

JShell is a new command-line interactive REPL (Read-Evaluate-Print-Loop) tool introduced in Java 9 to evaluate declarations, statements, and expressions written in Java. This tool also allows us to execute Java code snippets and get immediate results.Some times, we have the code already written in java file and able to execute it into JShell. To load a file into the JShell tool, we can use the "/open" command.For instance, I have created the "Test.java" file in the "c://temp" folder. Below is the code:String s1 = "TutorialsPoint"; String s2 = "Tutorix"; String s3 = s1 + s2; int sum(int a, int b) ... Read More

What is the JLink tool in Java 9?

raja
Updated on 27-Feb-2020 06:43:08

297 Views

JLink is a new linker tool that has been used to create our own customized JRE. Usually, we can run our program using default JRE provided by Oracle. If we need to create our own JRE then use this tool. JLink tool can help to create its own JRE with only the required class to run the application. It can reduce the size of the API developed and the dependency of using full JRE.In Java 9, we have a new phase between compiling the code and its execution link time. Link time is an optional phase between the phases of compile-time and runtime.Command to create custom JREjlink ... Read More

Importance of transferTo() method of InputStream in Java 9?

raja
Updated on 26-Feb-2020 13:20:24

1K+ Views

The transferTo() method has been added to the InputStream class in Java 9. This method has been used to copy data from input streams to output streams in Java. It means it reads all bytes from an input stream and writes the bytes to an output stream in the order in which they are reading.Syntaxpublic long transferTo(OutputStream out) throws IOExceptionExampleimport java.util.Arrays; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class TransferToMethodTest {    public void testTransferTo() throws IOException {       byte[] inBytes = "tutorialspoint".getBytes();       ByteArrayInputStream bis = new ByteArrayInputStream(inBytes);       ByteArrayOutputStream bos = new ... Read More

How to define control flow statements in JShell in Java 9?

raja
Updated on 26-Feb-2020 11:31:21

242 Views

JShell is a new interactive command-line tool introduced in Java 9. This tool can also be called REPL (Read-Eval-Print-Loop) because it takes input, evaluates it and returns output to the user via command-line.We can execute multiple-line control flow statements using JShell the same as Java. The control flow statements like If-else statement, for-loop and while-loop can also be executed in JShell. It recognizes multiple-line statements are prompts with the symbol “…>” to indicate to enter the next line statement.Example of If-Else statementjshell> int distance = 50 distance ==> 50 jshell> if(distance < 30) { ...>       System.out.println("It's near"); ...>    } ... Read More

What are the benefits of a module in Java 9?

raja
Updated on 26-Feb-2020 10:04:14

554 Views

An important feature introduced in Java 9 is Module. By using a module, we can divide the code into smaller components called modules. It means each module has its own responsibility and declare its dependency on other modules to work correctly.Below are the steps to create a modular project in Java 9:Initially, we can create a file named "module-info.java" and add to a package(module) for which it is created. For instance, if our package name is com.mycompany.mypackage then the file goes to the same package (src/com.mycompany.mypackage/module-info.java). We can create a module by declaring "exports" and "requires" expressions.If our modules require another module ... Read More

What are the characteristics of a module in Java 9?

raja
Updated on 26-Feb-2020 05:01:28

238 Views

The module is a collection of code, data, and resources. It is a set of related packages and types like classes, abstract classes, and interfaces with code, data files, and some static resources.Below are some of the characteristics of a module.Characteristics of a module:A module must define an interface for communication with other modules.A module defines the separation between a module interface and module implementation.A module presents a set of properties that contains information.Two or more modules have nested together.A module has a clear, defined responsibility. Each function implemented by only one module.A module must able to tested independently from other modules.An error in a module can't propagate to other ... Read More

Advertisements