In this tutorial, we will learn how to use JShell which is introduced in Java 9.
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.Why Use JShell?
Using JShell, you can enter program elements one at a time, immediately see the result, and make adjustments as needed.Java program development typically involves the following process:
- Write a complete program.
- Compile it and fix any errors.
- Run the program.
- Figure out what is wrong with it.
- Edit it.
- Repeat the process.
Requirements
Install JDK 9+
Launch JShell
rameshfadatare@Rameshs-MacBook-Air rameshfadatare % jshell
| Welcome to JShell -- Version 17.0.2
| For an introduction type: /help intro
JShell Intro
The JShell command /help, with a parameter intro, gives you basic guidelines on how you use the tool.jshell> /help intro
|
| intro
| =====
|
| The jshell tool allows you to execute Java code, getting immediate results.
| You can enter a Java definition (variable, method, class, etc), like: int x = 8
| or a Java expression, like: x + x
| or a Java statement or import.
| These little chunks of Java code are called 'snippets'.
|
| There are also the jshell tool commands that allow you to understand and
| control what you are doing, like: /list
|
| For a list of commands: /help
JShell Expressions
jshell> 2 + 5
$2 ==> 7
jshell> 5 - 2
$3 ==> 3
jshell> 10 * 5
$4 ==> 50
jshell> 10 / 2
$5 ==> 5
jshell> 9 % 3
$6 ==> 0
jshell> 9 % 2
$7 ==> 1
jshell> 10 + 20 + 5
$8 ==> 35
jshell> 20 - 10 * 2
$9 ==> 0
jshell> 20 * 2 - 10
$10 ==> 30
jshell> 15 * 2 / 30
$11 ==> 1
jshell> 10 + 20 + 30 * 2 - 10 / 50
$12 ==> 90
JShell Statements
jshell> System.out.println("hello world!");
hello world!
jshell> "java" + "guides"
$13 ==> "javaguides"
jshell> "java".equals("java")
$14 ==> true
jshell> "java guides".substring(0, 4)
$15 ==> "java"
Java Control Statements using JShell
if Statement
jshell> int x = 10;
...> int y = 12;
...> if(x+y > 20) {
...> System.out.println("x + y is greater than 20");
...> }
x ==> 10
y ==> 12
x + y is greater than 20
jshell> int x, y;
...> x = 10;
...> y = 20;
...> if (x < y) {
...> System.out.println("x is less than y");
...> }
...> x = x * 2;
...> if (x == y) {
...> System.out.println("x now equal to y");
...> }
...> x = x * 2;
...> if (x > y) {
...> System.out.println("x now greater than y");
...> }
...> // this won't display anything
...> if (x == y)
...> System.out.println("you won't see this");
x ==> 0
y ==> 0
x ==> 10
y ==> 20
x is less than y
x ==> 20
x now equal to y
x ==> 40
x now greater than y
if-else Statement
jshell> int x = 10;
...> int y = 12;
...> if(x+y < 10) {
...> System.out.println("x + y is less than 10");
...> } else {
...> System.out.println("x + y is greater than 20");
...> }
x ==> 10
y ==> 12
x + y is greater than 20
if-else-if Ladder
jshell> int month = 4; // April
...> String season;
...> if (month == 12 || month == 1 || month == 2)
...> season = "Winter";
...> else if (month == 3 || month == 4 || month == 5)
...> season = "Spring";
...> else if (month == 6 || month == 7 || month == 8)
...> season = "Summer";
...> else if (month == 9 || month == 10 || month == 11)
...> season = "Autumn";
...> else
...> season = "Bogus Month";
...> System.out.println("April is in the " + season + ".");
month ==> 4
season ==> null
April is in the Spring.
Nested if-statement
jshell> int num = 50;
...> if (num < 100) {
...> System.out.println("number is less than 100");
...> if (num == 50) {
...> System.out.println("number equal to 50");
...> if (num > 40) {
...> System.out.println("number is greater than 40");
...> }
...> }
...> }
num ==> 50
number is less than 100
number equal to 50
number is greater than 40
for Loop
jshell> //Code of Java for loop
...> for(int i=1;i<=10;i++){
...> System.out.println(i);
...> }
1
2
3
4
5
6
7
8
9
10
Nested for Loop
jshell> //loop of i
...> for(int i=1;i<=3;i++){
...> //loop of j
...> for(int j=1;j<=3;j++){
...> System.out.println(i+" "+j);
...> }//end of i
...> }//end of j
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
for-each Loop
jshell> //Declaring an array
...> int arr[]={12,23,44,56,78};
...> //Printing array using for-each loop
...> for(int i:arr){
...> System.out.println(i);
...> }
arr ==> int[5] { 12, 23, 44, 56, 78 }
12
23
44
56
78
while Loop
jshell> int i=1;
...> while(i<=10){
...> System.out.println(i);
...> i++;
...> }
i ==> 1
1
2
3
4
5
6
7
8
9
10
do-while Loop
jshell> int i=1;
...> do{
...> System.out.println(i);
...> i++;
...> }while(i<=10);
i ==> 1
1
2
3
4
5
6
7
8
9
10
Declare a Java Method
The Java Shell allows to declare a method without writing any enclosing class and invoke the method later.jshell> void printNumbers(int n) {
...> for(int i=0; i< n; i++) {
...> System.out.println(i);
...> }
...> }
...>
| created method printNumbers(int)
jshell> printNumbers(10);
0
1
2
3
4
5
6
7
8
9
jshell> void printSquaresOfNumbers(int n) {
...> for(int i=0; i< n; i++) {
...> System.out.println(i*i);
...> }
...> }
...>
| created method printSquaresOfNumbers(int)
jshell> printSquaresOfNumbers(10);
0
1
4
9
16
25
36
49
64
81
Declare a Java Class
Similarly to method declaration, you can declare a class by typing its code just like you write Java code in an editor, and the shell can detect when the class completes.jshell> public class Student {
...> private String name = "Ramesh";
...> private String college = "ABC";
...>
...> public Student() {
...> super();
...> }
...>
...> public Student(String name, String college) {
...> super();
...> this.name = name;
...> this.college = college;
...> }
...>
...> public String getName() {
...> return name;
...> }
...>
...> public void setName(String name) {
...> this.name = name;
...> }
...>
...> public String getCollege() {
...> return college;
...> }
...>
...> public void setCollege(String college) {
...> this.college = college;
...> }
...> }
| created class Student
jshell> Student student = new Student();
student ==> Student@2e5d6d97
jshell> System.out.println(student.getName());
Ramesh
jshell> System.out.println(student.getCollege());
ABC
Declare a Java Interface
Similar to the class declaration, you can declare an interface and the shell can detect when the declaration completes.jshell> public interface Vehicle {
...> String getBrand();
...>
...> String speedUp();
...>
...> String slowDown();
...>
...> default String turnAlarmOn() {
...> return "Turning the vehice alarm on.";
...> }
...>
...> default String turnAlarmOff() {
...> return "Turning the vehicle alarm off.";
...> }
...>
...> static int getHorsePower(int rpm, int torque) {
...> return (rpm * torque) / 5252;
...> }
...> }
| created interface Vehicle
Simple Array Example using JShell
jshell> // declares an array of integers
...> int[] anArray;
...>
...> // allocates memory for 10 integers
...> anArray = new int[10];
...>
...> // initialize first element
...> anArray[0] = 100;
...> // initialize second element
...> anArray[1] = 200;
...> // and so forth
...> anArray[2] = 300;
...> anArray[3] = 400;
...> anArray[4] = 500;
...> anArray[5] = 600;
...> anArray[6] = 700;
...> anArray[7] = 800;
...> anArray[8] = 900;
...> anArray[9] = 1000;
...>
...> for (int i = 0; i < anArray.length; i++) {
...> System.out.println("Element at index " + i + ": " +anArray[i]);
...> }
anArray ==> null
anArray ==> int[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
$65 ==> 100
$66 ==> 200
$67 ==> 300
$68 ==> 400
$69 ==> 500
$70 ==> 600
$71 ==> 700
$72 ==> 800
$73 ==> 900
$74 ==> 1000
Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000
Two Dimensional Array Example using JShell
jshell> String[][] names = {
...> {"Mr. ","Mrs. ","Ms. "},
...> {"Smith","Jones"}};
...>
...> // Mr. Smith
...> System.out.println(names[0][0] +
...> names[1][0]);
...> // Ms. Jones
...> System.out.println(names[0][2] +
...> names[1][1]);
names ==> String[2][] { String[3] { "Mr. ", "Mrs. ", "Ms. " ... [2] { "Smith", "Jones" } }
Mr. Smith
Ms. Jones
ArrayList Example using JShell
jshell> // Creating an ArrayList of String using
...> List<String> fruits = new ArrayList<>();
...> // Adding new elements to the ArrayList
...> fruits.add("Banana");
...> fruits.add("Apple");
...> fruits.add("mango");
...> fruits.add("orange");
...> System.out.println(fruits);
fruits ==> []
$80 ==> true
$81 ==> true
$82 ==> true
$83 ==> true
[Banana, Apple, mango, orange]
jshell> // Creating an ArrayList of String using
...> List<String> fruits = new ArrayList<>();
...> // Adding new elements to the ArrayList
...> fruits.add("Banana");
...> fruits.add("Apple");
...> fruits.add("mango");
...> fruits.add("orange");
...> fruits.add("Watermelon");
...> fruits.add("Strawberry");
...>
...> System.out.println("=== Iterate using Java 8 forEach and lambda ===");
...> fruits.forEach(fruit -> {
...> System.out.println(fruit);
...> });
fruits ==> []
$86 ==> true
$87 ==> true
$88 ==> true
$89 ==> true
$90 ==> true
$91 ==> true
=== Iterate using Java 8 forEach and lambda ===
Banana
Apple
mango
orange
Watermelon
Strawberry
View All the Code Snippets
jshell> /list
3 : public class Student {
private String name = "Ramesh";
private String college = "ABC";
public Student() {
super();
}
public Student(String name, String college) {
super();
this.name = name;
this.college = college;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
}
4 : public interface Vehicle {
String getBrand();
String speedUp();
String slowDown();
default String turnAlarmOn() {
return "Turning the vehice alarm on.";
}
default String turnAlarmOff() {
return "Turning the vehicle alarm off.";
}
static int getHorsePower(int rpm, int torque) {
return (rpm * torque) / 5252;
}
}
Drop a Snippet
Use /drop command to drop a snippet: Drop a interfacejshell> /drop Vehicle
| dropped interface Vehicle
jshell> /drop Student
| dropped class Student
jshell> public interface Vehicle {
...> String getBrand();
...>
...> String speedUp();
...>
...> String slowDown();
...>
...> default String turnAlarmOn() {
...> return "Turning the vehice alarm on.";
...> }
...>
...> default String turnAlarmOff() {
...> return "Turning the vehicle alarm off.";
...> }
...>
...> static int getHorsePower(int rpm, int torque) {
...> return (rpm * torque) / 5252;
...> }
...> }
| created interface Vehicle
jshell> /drop Vehicle
| dropped interface Vehicle
jshell> public class Student {
...> private String name = "Ramesh";
...> private String college = "ABC";
...>
...> public Student() {
...> super();
...> }
...>
...> public Student(String name, String college) {
...> super();
...> this.name = name;
...> this.college = college;
...> }
...>
...> public String getName() {
...> return name;
...> }
...>
...> public void setName(String name) {
...> this.name = name;
...> }
...>
...> public String getCollege() {
...> return college;
...> }
...>
...> public void setCollege(String college) {
...> this.college = college;
...> }
...> }
| created class Student
jshell> /drop Student
| dropped class Student
Exit JShell
jshell> /exit
| Goodbye
Comments
Post a Comment
Leave Comment