Using Variables in JShell of Java 9

Prerequisite : JShell in Java 9

We can declare variables and use anywhere throughout Jshell session. Let’s create an
integer variable.

jshell variable  example

Semicolon (;) is optional, we can leave it and it works fine. See, variable b is created without using semicolon.

jshell variable

Scratch Variables

If we don’t provide variable name, Java create implicit variable to store the value. These variables start with $ sign. We can use these variable by specifying implicit variable, as we did in the in the following screen-shot.

jhsell variable


We can test any valid Java expression to get instant output. See, the following example.
Adding two integers
jshell add

Compound expression

Creating and Calling Methods
To test method business logic, create a method and get result immediately. See, the following example

jshell method

jshell calling

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.