JShell
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.
Open JShell
$ jshellThe following error maybe shown
Unable to locate an executable at "~/.sdkman/candidates/java/current/bin/jshell" (-1)Verify the version of Java
$ java -version openjdk version "1.8.0_252" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.252-b09, mixed mode)JShell requires Java 9 or higher
| Welcome to JShell -- Version 14.0.1 | For an introduction type: /help intro jshell>To exit JShell, type
/exitjshell> /exitHelp
jshell> /help | Type a Java language expression, statement, or declaration. | Or type one of the following commands: | /list [<name or id>|-all|-start] | list the source you have typed | /edit <name or id> | edit a source entry | /drop <name or id> ... | rerun | a description of ways to re-evaluate previously entered snippetsCreate a variable
jshell> a = 7 | Error: | cannot find symbol | symbol: variable a | a = 7 | ^ⓘ NoteIn Java, variables require a type as Java is a statically typed language.jshell> int a = 7 a ==> 7Normally, a semicolon needs to be added at the end of the statement, indicating the end of statement (as defined by the JLS 14.8).
jshell> int a = 7; a ==> 7ⓘ NoteWhile the semicolon is optional in JShell, this is required in a Java program. The program will not compile if we ignore the semicolon at the end of the statement.Enter the variable name to print its value
jshell> a a ==> 7Java 10 introduced Local Variable Type Inference (JEP 286). Previously, all local variable declarations required an explicit (manifest) type on the left-hand side. With type inference, the explicit type can be replaced by the reserved type name
varfor local variable declarations that have initializers. The type of the variable is inferred from the type of the initializer.jshell> var x = 7 x ==> 7For more information about Local Variable Type Inference please refer to the style guidelines and article Java Feature Spotlight: Local Variable Type Inference by Brian Goetz.
ⓘ NoteOnce a variable is created, its type cannot be changed. Once we create anint, that variable will stay anintand cannot accept anything else.jshell> int x = 7 jshell> x = 7.7 | Error: | incompatible types: possible lossy conversion from double to int | x = 7.7 | ^-^Different from many other languages, Java is a statically typed language and we cannot compare the integer
1to the booleantruejshell> 1 == true | Error: | incomparable types: int and boolean | 1 == true | ^-------^Arithmetic Operations
jshell> int b = 3 b ==> 3 jshell> a + b $4 ==> 10The result is stored in a new temporary variable,
$4, (crated by JShell) which can be accessed as any other variable.jshell> $4 $4 ==> 10Use existing functionality
JShell imports automatically classes and functionality from the following packages
jshell> /imports | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* | import java.util.* | import java.util.concurrent.* | import java.util.function.* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.*For example, we can use the
Randomclass, found in thejava.utilpackage.jshell> var r = new Random() r ==> java.util.Random@533ddba jshell> r.nextInt(10) $3 ==> 8JShell has tab completion. Type
Math.and then press the[tab]keyjshell> Math. E IEEEremainder( PI abs( acos( addExact( asin( atan( atan2( cbrt( ceil( class copySign( cos( cosh( decrementExact( exp( expm1( floor( floorDiv( floorMod( fma( getExponent( hypot( incrementExact( log( log10( log1p( max( min( multiplyExact( multiplyFull( multiplyHigh( negateExact( nextAfter( nextDown( nextUp( pow( random() rint( round( scalb( signum( sin( sinh( sqrt( subtractExact( tan( tanh( toDegrees( toIntExact( toRadians( ulp( jshell> Math.You will see all methods (functionality) available to the
Mathclass.jshell> int a = 7 jshell> int b = 3 jshell> Math.max(a, b) $5 ==> 7We can import all methods available in the
Mathclass so that we can invoke these methods by their name.jshell> import static java.lang.Math.* jshell> max(a, b) $5 ==> 7 jshell> PI PI ==> 3.141592653589793A list of functions available in the Math class can be found here.
Add new functionality (a method)
jshell> int sum(int a, int b) { ...> return a+b; ...> } | created method sum(int,int)The created a method, named
sum(), takes two integers and returns their sum.jshell> sum(4,5) $7 ==> 9
For more information about JSHell, please refer to the JShell documentation.