Jshell java 9

JShell is an interactive tool that was introduced in java 9. JShell is also known as REPL stands for READ EVAL PRINT LOOP. In this article, we will look into JShell basics and how to run some programs in Jshell.

1. By using JShell, we can test the Java code without compiling using JAVAC. The JShell prints the result of calculations directly.
2. It is a tool that runs from the command line.

How to Start JShell

First, the JDK 9 should be installed on your machine. Now, there are two ways to start the JShell.

The first way to start Jshell

Step 1: Go to the location where JDK 9 is installed and look into /jdk-9/bin folder. Find the jshell.exe file in the folder.

Jshell java 9

Step 2: Double click on JShell and a new command window will appear.

Jshell java 9

The second way to Start JShell

Step1: Open the command prompt by use of the window button + R and type cmd.

Step2: Now check the java version to make sure you have java 9 or above, then only you can use JShell.

Check the version of Java by using the “Java -version”. After that, you can use the “JShell” command.

Write and Execute Code in JShell (REPL)

The Jshell (REPL) allows the developer to write and execute the code without creating and building a project.

Now, it’s time to execute some java examples. Let’s understand how it should be used.

Print statements:

To print a simple statement in JShell. You don’t need to create any class. For example, you want to print “Hello world”.

jshell> System.out.println(“Hello world”);

Output: Hello world

Variable:

You can declare variables like real programming and use them anywhere in the JShell session. In JShell you don’t have to write a class or main method.

Let’s create an integer variable:

jshell> int i = 10;

Here, we are creating a variable of int type that holds the value. After pressing the ENTER key. It shows the value.

i == > 10

Print the integer variable:

jshell> i

i == > 10

Let’s create a String variable:

jshell> String s = “Hello”;

Here, we are creating a variable of String type that holds the value. After pressing of the ENTER key. It shows the value.

s == > “Hello”

Print the String variable:

jshell> s

s == > “Hello”

Let’s create an Array :

jshell> int[] a = new int[2];

Here, we are creating an Array of int type with its size. After press of ENTER key. It shows the value of Array.

a ==> int[2] { 0, 0 }

Assigning value to Array at index 0:

jshell> a[0] = 1

$2 ==> 1

Assigning value to Array at index 0:

jshell> a[1] = 2

$3 ==> 2

Print the Array variable:

jshell> a

a ==> int[2] { 1, 2 }

Create method:

In JShell you can create a method as variables. To create a method in Jshell you must follow the syntax of the method declaration.

return_Type methodName(Parameteres)
{
 //Method body
}

To define a method in JShell you have to provide the return type, method name, parameters and method body. There is no need to define the Access modifiers.

For example:

jshell> void show(int a)
   ...> {
   ...> System.out.println("Value of a = "+a);
   ...> }
|  created method show(int)

jshell> int getData()
   ...> {
   ...> return 5;
   ...> }
|  created method getData()

How to call a method:

You can access the method to make a method call. You just need to write the method name and parameters(If the method has parameters).

jshell> show(5)

Value of a = 5

jshell> getData();

$5 ==> 5

How to check all defined methods:

By use of the methods command, you can check all methods. The JShell shows all the methods defined by the user.

jshell> /methods

|    void show(int)

|    int getData()

2 thoughts on “Jshell java 9”

  1. I was reading through some of your content on this site and I conceive this website is real informative! Keep on putting up. Ardys Kort Grazia

    Reply
  2. My brother recommended I may like this website. He used to be entirely right. This post actually made my day. You can not imagine simply how so much time I had spent for this information! Thanks!| Thekla Pavel Elvin

    Reply

Leave a Comment