Java Shell Tool (JShell)

Last updated on Dec 27 2022
Prabhas Ramanathan

It is an interactive Java Shell tool, it allows us to execute Java code from the shell and shows output immediately. JShell is a REPL (Read Evaluate Print Loop) tool and run from the command line.

Table of Contents

Advantages of JShell

Jshell has reduced all the efforts that are required to run a Java program and test a business logic.
If we don’t use Jshell, creating of Java program involves the following steps.
• Open editor and write program
• Save the program
• Compile the program
• Edit if any compile time error
• Run the program
• Edit if any runtime error
• Repeat the process
Jshell does not require above steps. We can evaluate statements, methods and classes, even can write hello program without creating class.

How to Start JShell

To start Jshell, first we must have installed Java 9 then open terminal in Linux or command prompt in windows and type jshell ?v. It will start jshell session and displays a welcome message to the console.

Hello Java Message

To display a simple “Hello Java” message, write print command without creating class and hit enter.

java 83

Variables

We can declare variables and use anywhere throughout Jshell session. Let’s create an integer variable.

java 84

Semicolon (;) is optional, we can leave it and it works fine. See, variable b is created without using semicolon.

java 85

Scratch Variables

If we don’t provide variable name, Java create implicit variable to store the value. These variables start with $ sign. We can use these variable by specifying implicit variable, as we did in the in the following screen-shot.

java 86

Expressions

We can test any valid Java expression to get instant output. See, the following example.

Adding two integers

1. jshell> 2+3 
2. $1 ==> 5 
3. | created scratch variable $1 : int 
4. jshell>

Compound expression

1. jshell> 2+(5*4+(2+1)) 
2. $4 ==> 25 
3. | created scratch variable $4 : int 
4. jshell>

Methods

To test method business logic, create an method and get result immediately. See, the following example.

1. jshell> void show(){ 
2. ...> System.out.println("This is show method"); 
3. ...> }

4. | created method show()

Calling method

1. jshell> show();
2. This is show method
To create class, write source code for the class and call its method by creating object immediately. See the following example.

 

Class

1. jshell> class Hello{ 
2. ...> void show(){ 
3. ...> System.out.println("This is show method"); 
4. ...> } 
5. ...> } 
6. | created class Hello 
7. 
8. jshell> new Hello().show(); 
9. This is show method

Package Imports

By default, 10 packages are imported and can also be imported any package by using import statement.
To see, default import packages, we can use following command.
1. jshell> /import
2. | import java.io.*
3. | import java.math.*
4. | import java.net.*
5. | import java.nio.file.*
6. | import java.util.*
7. | import java.util.concurrent.*
8. | import java.util.function.*
9. | import java.util.prefs.*
10. | import java.util.regex.*
11. | import java.util.stream.*

Importing java.sql package.

1. jshell> import java.sql.*;
Listing import packages and it will show available accessible packages.

1. jshell> /import
2. | import java.io.*
3. | import java.math.*
4. | import java.net.*
5. | import java.nio.file.*
6. | import java.util.*
7. | import java.util.concurrent.*
8. | import java.util.function.*
9. | import java.util.prefs.*
10. | import java.util.regex.*
11. | import java.util.stream.*
12. | import java.sql.*
Now number of packages are 11 including new one java.sql.*.

 

Jshell Commands

Jshell provides various useful commands that we can use to modify environment, manage code and to get code related information. Following are the useful information.

Package Imports

1. jshell> /import
2. | import java.io.*
3. | import java.math.*
4. | import java.net.*
5. | import java.nio.file.*
6. | import java.util.*
7. | import java.util.concurrent.*
8. | import java.util.function.*
9. | import java.util.prefs.*
10. | import java.util.regex.*
11. | import java.util.stream.*
12. | import java.sql.*
Command /vars to show variables.
1. jshell> /vars
2. | int $1 = 5
3. | int $2 = 5
4. | int $4 = 25

To get all written source code, use /list

1. jshell> /list 
2. 
3. 1 : 2+3 
4. 2 : 10-5 
5. 3 : $1 
6. 4 : 2+(5*4+(2+1)) 
7. 5 : void show(){ 
8. System.out.println("This is show method"); 
9. } 
10. 6 : show(); 
11. 7 : class Hello{ 
12. void show(){ 
13. System.out.println("This is show method"); 
14. } 
15. } 
16. 8 : new Hello().show(); 
17. 9 : import java.sql.*;

 

So, this brings us to the end of blog. This Tecklearn ‘Java Shell Tool’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Java Shell Tool (JShell)"

Leave a Message

Your email address will not be published. Required fields are marked *