JShell: Introduction To Java Shell Tool

Posted By : Neha Yadav | 28-May-2018

Java Shell Tool is an interactive tool to learn java and to do prototyping of java code.

It is in one of the good features of java 9.

In Jshell you don't have to make a class. You can directly type your statements, expressions. You can test your logic and explore more about your logic it.

To use it, you must have Java 9 installed on the system.

To open it, you have to type jshell in command line like this:-

$ jshell
|  Welcome to JShell -- Version 10.0.1
|  For an introduction type: /help intro

jshell> 

 There is no need to compile it and run , you can directly type the statement you want to check.

some examples to use it:-

1. Print Message.

jshell> System.out.println("Hello world");

Hello world

2. Sum two numbers

i) jshell> 1+1
$2 ==> 2

ii)jshell> int a=10; int b=20; a+b;
a ==> 10
b ==> 20
$5 ==> 30

iii)jshell> int a=10; int b=20; System.out.println(a+b);
a ==> 10
b ==> 20
30

3. Create a class, create its object and call its methods;

jshell> class A{ public void show(){System.out.println("hello world");}}
|  created class A

jshell> A a = new A();
a ==> A@3abbfa04

jshell> a.show();
hello world

All of these statements which we write and execute get stored in a file. We can edit that and store it as .java extension as per requirement.

1. To edit that file

jshell> /edit          

2. To save the file

jshell> /save filename.java

3. To exit from Jshell 

jshell> /exit

|  Goodbye

 

 

 

 

About Author

Author Image
Neha Yadav

Neha is a creative person. She is having good knowledge of core java, advance java, hibernate,spring boot. She likes to solve puzzles, sudoku. She is a fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..