Requirement jshell :

Immediate feedback is important when learning a programming language. The number one reason schools cite for moving away from Java as a teaching language is that other languages have a “REPL” and have far lower bars to an initial "Hello, world!" program. A Read-Eval-Print Loop (REPL) is an interactive programming tool which loops, continually reading user input, evaluating the input, and printing the value of the input or a description of the state change the input caused. Scala, Ruby, JavaScript, Haskell, Clojure, and Python all have REPLs and all allow small initial programs. JShell adds REPL functionality to the Java platform.

Exploration of coding options is also important for developers prototyping code or investigating a new API. Interactive evaluation is vastly more efficient in this regard than edit/compile/execute and System.out.println.

Let’s move towards examples:

Jshell Features

Following functionally provided by java 9 JShell:

  • Import Declaration
  • Class Declaration
  • Interface Declaration
  • Method Declaration
  • Field Declaration
  • Statement
  • Primary

1. JShell Statement

> jshell 10+5
$9 ==> 15

 

jshell-mathematical-calculation

jshell-mathematical-calculation

jshell> String companyName="Javadeveloperzone";
companyName ==> "Javadeveloperzone"

jshell> System.out.println(companyName);
Javadeveloperzone

2.  Jshell Class declaration

jshell > class Exployee {
 String name; 	
 Exployee(String name){
  this.name=name;
 }	 
 public String getName() { return this.name; } 
}
Exployee e=new Exployee("subhash");
e.getName();

 

jshell-create-class-example

jshell-create-class-example

3. Jshell method declaration:

public void methodUsingJSehll(){
System.out.println("I am created using JShell. I donot inside any class");
}

 

jshell-function-declaration

jshell-function-declaration

JShell does not support following things:

  • In a top-level declaration, the access modifiers (public, protected, and private), and the modifiers final and static are not allowed and are ignored with a warning; the modifiers default and synchronized are not allowed, and the modifier abstract is allowed only on classes.
  • The break, continue, and return statements have no appropriate context at the top-level, and are not allowed.
  • Package Declaration is not allowed. All JShell code is placed in the transient jshell package.
  • In JShell, each unique declaration key has exactly one declaration at any given time. For variables and classes the unique declaration key is the name, and, in support of overloading, the unique declaration key for methods is the name and the parameter types (to allow for overloading). As this is Java, variable, methods, and classes each have their own name spaces.

 

Ref: http://openjdk.java.net/jeps/222

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

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