Home> Java> javaTutorial> body text

How can we execute a code snippet by ID in JShell in Java 9?

WBOY
Release: 2023-08-30 22:17:02
forward
1274 people have browsed it

在Java 9的JShell中,我们如何通过ID执行代码片段?

JShellis an interactive tool (REPL) introduced in Java 9. We can execute snippets likeexpressions,variables,methods,classes, and etc without amain ()method in the JShell tool.

We can execute any prior snippet by simply typing/id, which indicates thesnippet’s ID. For instance, if we type "/1", then JShell can display thefirst snippetthat 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/1, /2, /3, and/4.

C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> 2+10 $1 ==> 12 jshell> String s = "Tutorialspoint" s ==> "Tutorialspoint" jshell> System.out.println("Tutorialspoint") Tutorialspoint jshell> int num1 = 25 num1 ==> 25 jshell> /1 2+10 $5 ==> 12 jshell> /2 String s = "Tutorialspoint"; s ==> "Tutorialspoint" jshell> /3 System.out.println("Tutorialspoint") Tutorialspoint jshell> /4 int num1 = 25; num1 ==> 25
Copy after login

在下面的代码段中,我们可以使用"/!"命令重新执行上一个代码段。

jshell> 2+5 $1 ==> 7 jshell> 10-6 $2 ==> 4 jshell> /1 2+5 $3 ==> 7 jshell> /! 2+5 $4 ==> 7
Copy after login

The above is the detailed content of How can we execute a code snippet by ID in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!