JShell is a Read-Evaluate-Print Loop (REPL) that evaluates declarations, statements, and expressions as we have entered and immediately shows the results. This tool is run from the command prompt.
In the below, we can define expressions, variables, and methods in JShell.
Expression
We can type any valid Java expression in JShell. The expression is either an arithmetic operation, string manipulation, and method call and evaluates immediately. All the results automatically assigned to a variable created by JShell. These variables have prefixed with $ symbol.
Example
jshell> 10 * 5 $1 ==> 50 jshell> 77 % 3 $2 ==> 2 jshell> $1 + $2 $3 ==> 52 jshell>
Variable
The Variables defined in JShell are the same as defined in a Java program. Once a variable is defined, it is present in the scope.
Example
jshell> String str = "Tutorialspoint" str ==> "Tutorialspoint" jshell> str str ==> "Tutorialspoint" jshell>
Method
We can define methods in JShell the same as how we can define in Java classes. Once a method has created in a JShell session, we can call it anytime until quitting that session.
Example
jshell> int sum(int x, int y) { ...> return x + y; ...> } | created method sum(int,int) jshell> sum(10,20) $2 ==> 30 jshell>