Define a Switch Statement in JShell in Java 9

raja
Updated on 23-Apr-2020 18:43:42

174 Views

JShell is based on the REPL (Read-Evaluate-Print-Loop) introduced in Java 9. This tool can be used to execute simple statements, evaluate it, and prints the result.A switch statement can test multiple conditions just like an else clause and handles the default possibility. The default clause can be executed when none of the cases match, and a break statement can be used to break out of switch after a successful match.In the below code snippet, we can define the switch statement in JShell.Snippet-1jshell> int i = 10; i ==> 10 jshell> switch(i) { ...> case 1 ... Read More

Use Terminal Stream Operations in JShell in Java 9

raja
Updated on 23-Apr-2020 14:28:12

252 Views

JShell is an interactive tool that takes simple statements, expressions and etc.. as input, evaluates it, and prints the result immediately to the user.Terminal Operation is a stream operation that takes a stream as input and doesn't return any output stream. For instance, a terminal operation can be applied to a lambda expression and returns a single result (A single primitive-value/object, or a single collection-of-objects). The reduce(), max(), and min() methods are a couple of such terminal operations.In the below code snippet, we can use different terminal operations: min(), max(), and reduce() methods in JShell.Snippetjshell> IntStream.range(1, 11).reduce(0, (n1, n2) -> n1 + n2); $1 ... Read More

Get Stack Trace Using Thread in Java 9

raja
Updated on 23-Apr-2020 12:19:48

504 Views

Java 9 has added StackWalker class to provide a standard API for accessing the current thread stack. In the previous java versions, we can use Throwable::getStackTrace, Thread::getStackTrace, and SecurityManager:: GetClassContext provided methods to obtain the thread stack.Thread.getStackTrace() method will return an array of stack trace elements representing the stack dump of a thread (StackTraceElement[]). The first element of an array represents the top of a stack, it can be the last method invocation in a sequence, and the last element of an array represents the bottom of a stack, it can be the first method invocation in a sequence.Syntaxpublic StackTraceElement[] ... Read More

Use Intermediate Stream Operations in JShell in Java 9

raja
Updated on 23-Apr-2020 09:49:02

334 Views

JShell is a tool introduced in Java 9, and it accepts simple statements like expressions, variables, methods, classes, etc.. as input and produces immediate results.A Stream is a sequence of values. An intermediate stream operation is an operation that takes a stream. For instance, it can be applied to a lambda expression and produces another stream of elements as its result.The most popular intermediate stream operations are mentioned below:1) sorted(): This method preserves the elements of the consumed stream as a result but also puts them in natural sorted order.2) distinct(): This method returns a stream retaining only unique elements of the input ... Read More

Implement Java Time LocalDate Using JShell in Java 9

raja
Updated on 22-Apr-2020 17:32:06

673 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 ==> 2020-04-22 jshell> today.getYear() $3 ==> ... Read More

Rules for the Publisher Interface in Java 9

raja
Updated on 22-Apr-2020 16:22:48

737 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

Raw String Literal in C++ Program

Sunidhi Bansal
Updated on 22-Apr-2020 12:47:24

615 Views

In this article, we will be discussing the raw string literal in C++, its meaning, and examples.There are escape characters in C++ like “” or “\t”. When we try to print the escape characters, it will not display on the output. To show the escape characters on the output screen we use a raw string literal by using R”(String with escape characters)”. After using R in the front of the string the escape character will be displayed on the output.ExampleLet’s understand this with the help of an example Live Demo#include using namespace std; int main(){    string str = "tutorialspoint" ... Read More

Modulus Function in C++ STL

Sunidhi Bansal
Updated on 22-Apr-2020 12:44:17

3K+ Views

In this article, we will be discussing the working, syntax, and examples of modulus functions in C++.What is modulus function C++?modulus function object class in C++, which is defined in header file. modulus function is a binary function object class used to get the result of the modulus operation of the two arguments. This function works the same as the operator ‘% ‘.Syntax of modulus functionTemplate struct modulus : binary_function {    T operator() (const T& a, const T& b) const {return a%b; } };Template parametersThe function accepts the following parameter(s) −T − This is the type of the ... Read More

Logical AND in C++

Sunidhi Bansal
Updated on 22-Apr-2020 12:42:28

120 Views

In this article, we will be discussing the working, syntax, and examples of logical_and function object class in C++.What is logical_and?logical_and binary function is an inbuilt binary function object class in C++, which is defined in a header file. logical_and is a binary function used to give the result of logical “and” operation between two arguments.Logical AND is the binary operation that returns true only and only if both the binary values are true.Syntax of logical_andTemplate struct logical_and : binary_function {    T operator() (const T& a, const T& b) const {return a&b&; } };Template parametersThe function accepts the ... Read More

ilobg Function in C++ STL

Sunidhi Bansal
Updated on 22-Apr-2020 12:40:37

93 Views

In this article, we will be discussing the working, syntax, and examples of ilogb() function in C++.What is ilogb()?ilogb() function is an inbuilt function in C++ STL, which is defined in iostream> using namespace std; int main(){    int output, var = 2;    output = ilogb(var);    cout

Advertisements