Tutorial by Examples

Starting JShell Before trying to start JShell, make sure your JAVA_HOME environment variable points to a JDK 9 installation. To start JShell, run the following command: $ jshell If all goes well, you should see a jshell> prompt. Exiting JShell To exit JShell, run the following command from...
Within JShell, you can evaluate Java expressions, with or without semicolons. These can range from basic expressions and statements to more complex ones: jshell> 4+2 jshell> System.out.printf("I am %d years old.\n", 421) Loops and conditionals are fine, too: jshell> for (int...
You can declare local variables within JShell: jshell> String s = "hi" jshell> int i = s.length Keep in mind that variables can be redeclared with different types; this is perfectly valid in JShell: jshell> String var = "hi" jshell> int var = 3 To see a list...
You can define methods and classes within JShell: jshell> void speak() { ...> System.out.println("hello"); ...> } jshell> class MyClass { ...> void doNothing() {} ...> } No access modifiers are necessary. As with other blocks, semicolons are require...
The basic unit of code used by JShell is the snippet, or source entry. Every time you declare a local variable or define a local method or class, you create a snippet whose name is the identifier of the variable/method/class. At any time, you can edit a snippet you have created with the /edit comman...

Page 1 of 1