Using Variables in JShell of Java 9

JavaServer Side ProgrammingProgramming

In JShell 9, variables can be declared during a session. Once the user has logged into the session, they can declare a variable as follows −

jshell> int val = 56 ;

Italics indicate the terminal, once the user has logged in to their session.

The above line would print the below output. The semicolon in the above line is optional, and it will run fine without the semicolon also.

Output

val = = > 56

When an integer value is defined by assigning it to a variable name on the JShell, and it is executed, by pressing ‘Enter’ key, it is displayed on the next line of JShell command line.

Incase we don’t assign a variable to a value and just print it on the JShell, it will assign a variable to the value −

jshell> 79

Output

$3 = = > 79

When an integer value is defined without assigning it to a variable name on the JShell, and it is executed, by pressing ‘Enter’ key, it is displayed on the next line of JShell command line. Here, JShell itself assigns a variable name to the most recently entered variable value.

raja
Published on 09-Jul-2020 10:16:30
Advertisements