First Program with JShell, Hello World using JShell

  In this JShell tutorial, we will learn how to print Hello World using JShell Tool, Learn JShell to print a statement using Method or Function.

JShell First Program

JShell is an interactive tool. We need to set an environment of Java JDK 9 or above in order to use JShell Tool.

JShell Hello World.


Let’s have a quick look at the First JShell Program:

jshell> System.out.print("Hello, JShell !");
Hello, JShell !
jshell>

Above java program code simply run the print statement because in JShell we don’t need to write full code. Just write a one-line of statement and execute it.

Table of Contents

JShell Print Hello World Using Method

In JShell, you can write method as you write in java program as shown below, We need to write line by line statement as :

  1. void display(){
  2. System.out.println(“Hello, Codenaive”);
  3. }
  4. display();

Example 1 : Print “Hello World” In JShell using Function

   
 
jshell> void display(){
   ...>     System.out.println("Hello, Codenaive");
   ...> }
|  created method display()

jshell> display();
Hello, Codenaive

jshell>

Example 2 : Print “Hello World” In JShell using Function

   
 
jshell> void display(){
   ...>     System.out.println("Hello, World");
   ...> }
|  created method display()


jshell> void outerDisplay(){
   ...>     display();
   ...> }
|  created method outerDisplay()

jshell> outerDisplay();
Hello, World

jshell>

Example 3 : Print “Hello World” In JShell using Function

   
 
jshell> String display(){
   ...>     return "Hello, World";
   ...> }
|  created method display()

jshell> System.out.print(display());
Hello, World

jshell>

Leave a Reply

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