Found 2616 Articles for Java

Importance of Module Descriptor in a Module in Java 9?

raja
Updated on 26-Mar-2020 10:44:32

625 Views

A module is a collection of code in the form of classes organized in packages and static resources such as property files or others. It provides the outside environment with all the information that can be required to use that module. The module descriptor is a key source of the module system, and it's compiled version of a module declaration specified in a file named "module-info.java" file at the root of the module’s directory hierarchy.The module describes itself by a module declaration as belowmodule com.myproject.module1 {    requires com.myproject.module2;    exports com.myproject.project1;    exports com.myproject.project2; }Below are some of the module descriptors described:module module. name: declares a module ... Read More

What are the different Http/2 Client classes in Java 9?

raja
Updated on 25-Mar-2020 15:40:30

108 Views

Http/2 is the newer version of the Http protocol. The improvements of Http/2 include focusing on how data is framed and transported between server and client. In this new version of the Http/2 protocol, separate classes have defined for the Http client, requests, and responses. The new API makes Http connections more easily maintained, faster, and allows for a more responsive application without the need for third-party libraries.The new API handles HTTP connections through three classes.HttpClient: It handles the creation and sending of requests.HttpRequest: It is used to construct a request to be sent via the HttpClient.HttpResponse: It holds the ... Read More

Why @SafeVarargs is required in Java 9?

raja
Updated on 24-Mar-2020 13:53:26

174 Views

The varargs functionality has been introduced in Java to facilitate the creation of methods with a variable number of arguments without resorting to array-type parameters or overloaded versions of the same method.Before Java 9 versions, if vararg methods are used with generics, then there is a warning message. Even though not all methods create heap pollution, compiler shows warning for all vararg methods used with generics. That's the reason @SafeVarargs concept was added to Java 9 version to avoid these warnings. If we added this annotation, then the compiler stops these warnings.We can compile the code by using the below commandjavac -Xlint:unchecked ... Read More

Can we have a private method or private static method in an interface in Java 9?

raja
Updated on 23-Mar-2020 16:47:33

3K+ Views

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only. We can't access or inherit private methods from one interface to another interface or class.Syntaxinterface {    private static void methodName() {       // some statements    }    private void methodName() {       // some statements    } }Exampleinterface Java9Interface {    public abstract void method1();    public default void method2() {       method4();       method5(); ... Read More

How to re-execute existing snippets in JShell in Java 9?

raja
Updated on 23-Mar-2020 13:36:57

123 Views

JShell is the first REPL tool introduced in Java 9. We can able to execute simple snippets in a command-line prompt by using JShell tool. We can start a JShell session by typing "jshell" command, stop the session by typing "/exit" command, and search for particular command by using "/help" command.The "/reload" command can be used to re-execute all existing snippets in JShell. We can also remove all prior code from a JShell session by using the "/reset" command.In the below code snippet, we have created a set of snippets.jshell> 2+10 $1 ==> 12 jshell> String s = "Tutorialspoint" s ... Read More

Differences between Java 8 and Java 9?

raja
Updated on 23-Mar-2020 10:56:46

2K+ Views

Java 9 version has introduced new enhancements and added new features. It includes JShell, Http2Client, Java Platform Module System (JPMS), Multi-release jar files, Stack Walking API, Private methods in an interface, Process API updates, Collection API updates, Stream API improvements, and etc.Below are the few differences between Java 8 and Java 9In Java 8 and earlier versions, the top-level component is the package. It places a set of related types (classes, interfaces, enums, and etc) into a group, and also contains a set of resources whereas Java 9 introduces a new component: module, which can be used to place a ... Read More

How can we execute snippets by ID in JShell in Java 9?

raja
Updated on 23-Mar-2020 06:40:39

152 Views

JShell is an interactive tool (REPL) introduced in Java 9. We can execute snippets like expressions, variables, methods, classes, and etc without a main () method in the JShell tool.We can execute any prior snippet by simply typing /id, which indicates the snippet’s ID. For instance, if we type "/1", then JShell can display the first snippet that we have entered, executes it, and shows the result. We can re-execute the last snippet that we have entered (whether it is valid or invalid) by using "/!".In the below code snippet, we have created a set of snippets, and execute those snippets by using ... Read More

How can we avoid compilation errors in JShell in Java 9?

raja
Updated on 20-Mar-2020 18:02:14

150 Views

JShell is an interactive tool that enables us to execute java code and displays the output instantly. JShell is the REPL(Read-Evaluate-Print-Loop) tool that runs from the command-line prompt. If we need to avoid the compilation-errors in JShell, then we must declare those variables before using it. The error message in JShell can use the notation "^--^" to highlight an error.In the below code snippet, the declaration of an int variable "div" attempts to use variables: num1, and num2 that has not been declared, so JShell reports a compilation error, indicating that the compiler was unable to find those variables.C:\Users\User>jshell | Welcome to JShell ... Read More

How can we implement a map in JShell in Java 9?

raja
Updated on 20-Mar-2020 08:09:49

226 Views

JShell is a java shell tool that has introduced in Java 9. It is an interactive tool that reads input, executes it, and prints it in the command-line prompt. We don't need to write a main() method to execute it like Java class.We can implement different collections includes set, list, and map in the JShell tool. The important collection is the Map interface, and it is a key-value pair. A Map doesn't contain duplicate keys and each key maps to at most one value.In the below example, we can able to implement the non-empty map.C:\Users\User>jshell | Welcome to JShell -- ... Read More

What are the components in a module-info file in Java 9?

raja
Updated on 20-Mar-2020 07:07:25

1K+ Views

A module is an independent unit of application that represents a single functionality. A module contains three important componentsName: To uniquely identify itDependencies: Other modules in which it depends onExported packages: Packages which are open for external applicationIn order to declare a module, we need to add the "module-info.java" file to the root source code. The components of the "module-info.java" file includes "name", "requires", "exports", and "exports to".Below is the template of "module-info.java" filemodule {    requires ;    requires ;    ...    exports ;    exports ;    ...    exports to ; }Name: It is ... Read More

Advertisements