How can we avoid compilation errors in JShell in Java 9?


JShell is an interactive tool that enables us to execute java code and displays the output instantly. JShell is the REPL(Read-Evaluate-Print-Loop) tool that runs from the command-line prompt. If we need to avoid the compilation-errors in JShell, then we must declare those variables before using it. The error message in JShell can use the notation "^--^" to highlight an error.

In the below code snippet, the declaration of an int variable "div" attempts to use variables: num1, and num2 that has not been declared, so JShell reports a compilation error, indicating that the compiler was unable to find those variables.

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> int div = num1 / num2
|  Error:
|  cannot find symbol
|  symbol: variable num1
|  int div = num1 / num2;
|  ^--^
|  Error:
|  cannot find symbol
|  symbol: variable num2
|  int div = num1 / num2;
|  ^--^

In the below code snippet, JShell displays the variable’s name: num1 and num2 followed by double equal and greater-than signs (==>).

jshell> int num1 = 35
num1 ==> 35

jshell> int num2 = 7
num2 ==> 7

jshell> int div = num1 / num2
div ==> 5

Updated on: 20-Mar-2020

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements