search
HomeJavajavaTutorialHow to define expressions, variables and methods in JShell in Java 9?

在Java 9中如何在JShell中定义表达式、变量和方法?

JShell 是一个读取-评估-打印循环(REPL),它会在我们输入的声明语句表达式上立即进行评估并显示结果。该工具从命令提示符中运行。

在下面的示例中,我们可以在JShell中定义表达式、变量和方法。

表达式

我们可以在JShell中输入任何有效的Java表达式。该表达式可以是算术运算字符串操作方法调用,并立即进行评估。所有的结果都会自动分配给JShell创建的变量。这些变量的前缀是 $符号。

示例

<strong>jshell> 10 * 5
$1 ==> 50

jshell> 77 % 3
$2 ==> 2

jshell> $1 + $2
$3 ==> 52

jshell></strong>

变量

JShell中定义的变量与Java程序中定义的变量相同。一旦定义了变量,它就存在于作用域中。

示例

<strong>jshell> String str = "Tutorialspoint"
str ==> "Tutorialspoint"

jshell> str
str ==> "Tutorialspoint"

jshell></strong>

方法

我们可以在JShell中定义方法,就像在Java类中定义方法一样。一旦在JShell会话中创建了一个方法,我们可以随时调用它,直到退出该会话。

示例

<strong>jshell> int sum(int x, int y) {
   ...> return x + y;
   ...> }
| created method sum(int,int)

jshell> sum(10,20)
$2 ==> 30

jshell></strong>

The above is the detailed content of How to define expressions, variables and methods in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.