Found 2611 Articles for Java

What is Project Jigsaw in Java 9?

raja
Updated on 04-Mar-2020 08:16:00

713 Views

The primary objective of the Jigsaw project is to introduce the modularity concept to create a module in Java 9 and then applying the same to JDK.Below are some of the benefits of Modularity(Jigsaw)Strong Encapsulation: The modules can access only those parts that can be available for use. Unless the package is explicitly exported into the module-info.java file, public classes in a package can't be public.Clear Dependencies: A module must declare about other modules that they are used through the required clause. The modules are combined in order to create a shorter runtime that can be scaled to comparatively smaller computing ... Read More

When to use the readNBytes() method of InputStream in Java 9?

raja
Updated on 03-Mar-2020 14:12:22

1K+ Views

Since Java 9, the readNBytes() method can be added to the InputStream class. This method reads the requested number of bytes from an input stream into the given byte array. This method blocks until len bytes of input data have read, end of a stream is detected, or an exception is thrown. The readNBytes() method doesn't close an input stream. This method can be useful to avoid memory problems with large files.Syntaxpublic int readNBytes(byte[] b, int off, int len) throws IOExceptionIn the below example, we have created a "Technology.txt" file in the source folder with simple data: { "JAVA", "PYTHON", ... Read More

What are the different compilation modes of a module in Java 9?

raja
Updated on 03-Mar-2020 08:35:30

210 Views

A module is a container of packages and each module contains a module descriptor that includes module name, module dependencies,  it means that the name of other modules depends on and name of the packages it exports that can be used only by modules that depend on it.module com.tutorialspoint.app {    /** Modules upon which the module com.tutorialspoint.app depends on */    requires com.tutorialspoint.services;    /** Packages exposed by this module which can be used by other modules */    exports com.tutorialspoint.app.util; }There are three different compilation modes provided by Java 9 Module: Legacy mode, single module mode, and multi-module mode.Compilation modes of ... Read More

How to declare a class and an interface in JShell in Java 9?

raja
Updated on 03-Mar-2020 05:46:13

260 Views

JShell can provide an interactive shell for quickly prototyping, debugging, and learning Java and Java API without the need for the main() method or need to compile our code before executing it.Declaration of Class:We can declare a class just like we have written a code in Java Language. The JShell can detect when the class completes it.In the below code snippet, we can declare a class Employee with two parameters and one method.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> class Employee { ...>       String empName; ...>       ... Read More

When to use the readAllBytes() method of InputStream in Java 9?

raja
Updated on 28-Feb-2020 19:23:12

7K+ Views

Since Java 9, we can use the readAllBytes() method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.The reallAllBytes() method can't automatically close the InputStream instance. When it can reach the end of a stream, the further invocations of this method can return an empty byte array. We can use this method for simple use cases where it is convenient to read all bytes into a byte array and not ... Read More

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

363 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

Advertisements