public class <em>SomethingOrOther</em> {
public static void main(String args[>) {
A Java program requires this verbose introduction because
In Java the entire program is a class.
The main
method is called into action automatically when the program begins running.
Anyway, retyping this boilerplate code into an editor window can be annoying, especially when your goal is to test the effect of executing a few simple statements. To fix this problem, the stewards of Java came up with a new tool in Java 9. They call it JShell.
Instructions for launching JShell differ from one computer to the next. For instructions that work on your computer, visit allmycode.com.
When you use JShell, you hardly ever type an entire program. Instead, you type a Java statement, and then JShell responds to your statement, and then you type a second statement, and then JShell responds to your second statement, and then you type a third statement, and so on. A single statement is enough to get a response from JShell.
JShell is only one example of a language's Read Evaluate Print Loop (REPL). Many programming languages have REPLs and, with Java 9, the Java language finally has a REPL of its own.
Here, JShell was used to find out how Java responds to assignment statements.
When you run JShell, the dialogue goes something like this:
jshell> <em>You type a statement</em>
<em>JShell responds</em>
jshell> <em>You type another statement</em>
<em>JShell responds</em>
For example, you can type double amountInAccount
and then press Enter. JShell responds by displaying
amountInAccount ==> 0.0
Here are a few things to notice about JShell:
- You don't have to type an entire Java program.
Typing a few statements such as
double amountInAccount
amountInAccount = 50.22
amountInAccount = amountInAccount + 1000000.00
does the trick.
- In JShell, semicolons are (to a large extent) optional.
A semicolon was typed at the end of only one of the nine lines.
- JShell responds immediately after you type each line.
After amountInAccount
was declared to be double
, JShell responds by saying that the amountInAccount
variable has the value 0.0. After amountInAccount = amountInAccount + 1000000.00
is typed, Shell responds that the new value of amountInAccount is 1000050.22
.
- You can mix statements from many different Java programs.
- You can ask JShell for the value of an expression.
You don't have to assign the expression's value to a variable. For example, type
elevatorWeightLimit / weightOfAPerson
JShell responds that the value of
elevatorWeightLimit / weightOfAPerson
is 9. JShell makes up a temporary name for that value. Above, the name happens to be
$8
. So, on the next line, when asked for the value of
$8 +1
, JShell gives the answer 10.
- You can even get answers from JShell without using variables.
On the last line, the value of 42 + 7 is requested, and JShell generously answers with the value 49.
While you're running JShell, you don't have to retype commands that you've already typed. If you press the up-arrow key once, JShell shows you the command that you typed most recently. If you press the up-arrow key twice, JShell shows you the next-to-last command that you typed. And so on. When JShell shows you a command, you can use your left- and right-arrow keys to move to any character in the middle of the command. You can modify characters in the command. Finally, when you press Enter, JShell executes your newly modified command.
To end your run of JShell, you type /exit (starting with a slash). But/exit
is only one of many commands you can give to JShell. To ask JShell what other kinds of commands you can use, type /help.With JShell, you can test your statements before you put them into a full-blown Java program. That makes JShell a truly useful tool.
Visit allmycode.com for instructions on launching JShell on your computer. After launching JShell, type a few lines of code. See what happens when you type some slightly different lines.