Found 2616 Articles for Java

How to show reflection frames of StackFrame in Java 9?

raja
Updated on 24-Apr-2020 10:22:23

126 Views

A standard API has been provided in Java 9 using java.lang.StackWalker class. This class designed to be efficient by allowing lazy access to the stack frames. A couple of other options allow in a stack trace that includes implementation and/or reflection frames, and it can be useful for debugging purposes. For instance, we add SHOW_REFLECT_FRAMES option to StackWalker instance upon creation, so that frames for reflective methods are printed as well.In the below example, we can able to show reflection frames of StackFrameExampleimport java.lang.StackWalker.Option; import java.lang.StackWalker.StackFrame; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; public class ReflectionFrameTest {    public static ... Read More

How to skip certain classes in StackFrame in Java 9?

raja
Updated on 24-Apr-2020 09:20:53

148 Views

StackWalker API has been introduced in Java 9, and it gives a snapshot of the stack trace of current thread at any given point of time and has methods to walk over it. The advantage of using StackWalker class over Thread::getStackTrace() is to filter or skip certain classes and get the instance of declaring the class itself and get either short stack trace or full stack trace instead of pulling the complete stack trace itself.In the below example, we can use java.util.stream.Stream.skip() method to skip Stack Frames.Exampleimport java.lang.StackWalker.*; import java.util.Optional; import java.util.List; import java.util.stream.Collectors; import java.lang.StackWalker.StackFrame; public class StackWalkerSkipTest {    public ... Read More

How to define a switch statement in JShell in Java 9?

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

110 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

How to use terminal stream operations in JShell in Java 9?

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

170 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

How to get stack trace using thread in Java 9?

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

369 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

How to use intermediate stream operations in JShell in Java 9?

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

217 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

How to implement java.time.LocalDate using JShell in Java 9?

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

447 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

What are the rules for the Publisher interface in Java 9?

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

554 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

How to implement relational and logical operators in JShell in Java 9?

raja
Updated on 22-Apr-2020 11:11:58

111 Views

JShell has introduced in Java 9 that enables us to explore, discover, and experiment with Java language features, and extensive libraries.The relational operators (==, != , =) can be used mainly for comparison. It accepts operands of non-boolean primitive data types and returns a boolean value. JShell also supports logical operators that can be used in expressions. The logical operators can expect boolean operands. The expressions involving these operands can be used for forming boolean conditions in the code within if, for, and while statements. The logical operators include : "&& : logical AND", "|| : OR" and "! : NOT".In the below two code snippets, we can ... Read More

What are the rules for the Subscription interface in Java 9?

raja
Updated on 22-Apr-2020 08:10:29

260 Views

A Subscription can be shared by exactly one Publisher and one Subscriber for the purpose of mediating data exchange. That is the reason subscribe() method doesn't return created Subscription, instead returns void. The Subscription is only passed to Subscriber through the onSubscribe() method callback. The Subscription interface contains two methods: request() and cancel().Syntaxpublic interface Subscription {    public void request(long n);   public void cancel(); }Rules for Subscription interface:Subscription.request() and Subscription.cancel() methods must be called only inside of its Subscriber context.Subscription must allow Subscriber to call the Subscription.request() method synchronously from within onNext() or onSubscribe() methods.Subscription.request() method must place an upper ... Read More

Advertisements