Home > Java > javaTutorial > How to define switch statement in JShell in Java 9?

How to define switch statement in JShell in Java 9?

王林
Release: 2023-08-20 20:49:02
forward
990 people have browsed it

在Java 9的JShell中如何定义switch语句?

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-1

<strong>jshell> int i = 10;
i ==> 10

jshell> switch(i) {
   ...>    case 1 : System.out.println("1");
   ...>    case 10 : System.out.println("10");
   ...>    default : System.out.println("default");
   ...> }
10
default

jshell> int i = 1;
i ==> 1

jshell> switch(i) {
   ...>    case 1 : System.out.println("1");
   ...>    case 10 : System.out.println("10");
   ...>    default : System.out.println("default");
   ...> }
1
10
default</strong>
Copy after login

在下面的代码片段中,我们可以在JShell中定义一个带有break的switch语句

Snippet-2

<strong>jshell> switch(i) {
   ...>    case 1 : System.out.println("1"); break;
   ...>    case 10 : System.out.println("10"); break;
   ...>    default : System.out.println("default"); break;
   ...> }
1</strong>
Copy after login

The above is the detailed content of How to define switch statement 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template