Object Oriented Programming Articles

Page 476 of 589

Why @SafeVarargs is required in Java 9?

raja
raja
Updated on 24-Mar-2020 322 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 ...

Read More

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

raja
raja
Updated on 23-Mar-2020 228 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" ...

Read More

Differences between Java 8 and Java 9?

raja
raja
Updated on 23-Mar-2020 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
raja
Updated on 23-Mar-2020 246 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 ...

Read More

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

raja
raja
Updated on 20-Mar-2020 251 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 | ...

Read More

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

raja
raja
Updated on 20-Mar-2020 2K+ 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

How can we display all module names in Java 9?

raja
raja
Updated on 19-Mar-2020 412 Views

In Java 9, the module concept has introduced. It is a named, self-describing collection of code and data. The code can be organized as a set of packages containing types like java classes and interfaces, and data includes resources and other kinds of static information. A module contains a name, dependencies, and exported packages.Syntaxmodule com.tutorialspoint.mymodule { // some statements }In the below example, we can able to display all module names by using the ModuleLayer class.Examplepublic class AllModulesNamesTest { public static void main(String args[]) { ModuleLayer.boot().modules().forEach((module) -> { ...

Read More

How to print the pattern of stars in JShell in Java 9?

raja
raja
Updated on 19-Mar-2020 331 Views

JShell is a REPL tool introduced in Java 9 that allows us to execute Java code and getting results immediately. We can evaluate expressions or simple algorithms without creating a new project, compile or build it by using JShell. We can also execute expressions, use imports, define classes, methods, and variables. It is a part of Java 9 JDK but not JRE.We can start JShell session in command-prompt by simply typing jshell. We can use different commands: /exit to quit the JShell session, reset/reload JShell anytime by typing /reset, and /reload, /import to list the imports, etc.In the below example, ...

Read More

What are Compact Strings in Java 9?

raja
raja
Updated on 19-Mar-2020 432 Views

Since Java 9, the JVM optimizes strings by using a new feature called Compact Strings. Instead of having a char[] array, a string can be represented as a byte[] array. We can use either UTF-16 or Latin-1 to produce either one or two bytes per character. If JVM detects the string contains only ISO-8859-1/Latin-1 characters, then string uses one byte per character internally.The string can be represented with a compact string or not is detected when the string is created. This feature has enabled by default and switches off using the -XX:-CompactStrings. It doesn't revert to a char[] implementation and ...

Read More

How to display all stack frames of the current thread in Java 9?

raja
raja
Updated on 19-Mar-2020 486 Views

Stack Walking API can provide a flexible mechanism to traverse and extract information from call stacks that allow us to filter and access frames in a lazy manner. StackWalker class is an entry point to Stack Walking API. The stack trace is a representation of a call stack at a certain point of time in which each element represents a method invocation. It contains all invocations from the start of a thread until the point it’s generated.In the below example, we can print/display all stack frames of the current thread by using StackWalker API.Exampleimport java.lang.StackWalker.StackFrame; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; public ...

Read More
Showing 4751–4760 of 5,881 articles
« Prev 1 474 475 476 477 478 589 Next »
Advertisements