If you have installed Java 9 on your machine you can play with jshell ( interactive java console).

In this tutorial:

  • how to start jshell
  • basic commands
  • unassigned and assigned variables
  • classes
  • methods
  • if and for loop
  • imports
  • jshell autocompletion

If you need to install java 9 you can find how to donwload and install it here:

start console

The jshell can be started by command:

jshell

result:

test@test:~$ jshell
|  Welcome to JShell -- Version 9-internal
|  For an introduction type: /help intro

basic commands in jshell

Here you can find several useful commands while working with jshell:

  • /exit - if you want to quit current session. Another option for Ubuntu shell is by pressing: CTRL+Z
  • /vars - list of all jshell variables - unassigned and assigned(check section variables). Result:
-> /vars
|    int x = 10
|    String $2 = "Java 9"
|    Test ts = Test@42e26948
|    int $11 = 10
|    int $12 = 10
  • /methods - list of jshell methods
-> /methods
|    printf (String,Object...)void
|    printX ()void
  • /imports - all jshell imports
-> /imports 
|    import java.util.*
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.util.concurrent.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.Arrays
  • /classes - all jshell classes
-> /classes
|    class Test
  • /list - jshell snippets if any
-> /list 

   1 : int x = 10;
   2 : "Java 9"
   3 : System.out.println($2);
   4 : class Test {
       }
   5 : Test ts = new Test();
   6 : System.out.println(ts);
   7 : void printX(){
       System.out.println("X");
       }
   8 : printX()
   9 : import java.util.Arrays;
  10 : if(x > 0){
       System.out.println(x);
       }
  11 : 10

assigned and unassigned/temporary variables

This is how assigned variable is declared ( as you notice you don't need to put semicolons):

  • assigned variable
-> int x = 10
|  Added variable x of type int with initial value 10
  • unassigned variable
-> "Java 9"
|  Expression value is: "Java 9"
|    assigned to temporary variable $2 of type String

In order to access temporary variable you need to write: $2

-> System.out.println($2);
Java 9

Define classes in jshell

If you want to add new empty class Test then you need to write only: class Test {}

-> class Test {
>> }
|  Added class Test

creating new intance is the same as in java and the access to the class methods, variables:

-> Test ts = new Test()
|  Added variable ts of type Test with initial value Test@42e26948

-> System.out.println(ts);
REPL.$REPL12$Test@42e26948

Method declaration in jshell

Declaring new method can be done the same way as a class. You can start to type the method line by line using semicolons and enter:

-> void printX(){
>> System.out.println("X");
>> }
|  Added method printX()

You need to use semicolons with methods.

Executed method in jshell

-> printX()
X

Jshell autocompletion

If you use TAB you will find all possible commands starting with current text:

-> print
printX()   printf(    

Jshell imports

You can add new imports to jshell by:

import java.util.Arrays;

To view all imports: /imports:

-> /imports
|    import java.util.*
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.util.concurrent.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.Arrays

Jshell for and if

if in jshell with assigned variable:

-> if(x > 0){
>> System.out.println(x);
>> }
10

for loop:

-> for(int i=0;i<5;i++){
>>  System.out.println(i);
>> }
0
1
2
3
4