java 9 - topjavatutorial.com

JShell – Java 9 REPL

JShell

JDK 9 is planning to add JShell for REPL(Read-Eval-Print Loop) functionality in Java platform.

JShell is a command-line tool for quickly running statements.

JShell provides a way to interactively evaluate declarations, statements, and expressions in Java. So, we don’t need to create a class with main method to test some code.

 

Getting started with JShell

To get started with JShell, download JDK 9 from https://jdk9.java.net/

Once you have updated your JAVA_HOME to point to your JDK 9 installation, you can type “jshell” in command prompt to get started.

To exit JShell, type /exit and hit enter

You can also check all the available commands using /help

jshell getting started
 
Variables & Expressions

You can declare variables and type expressions involving them to see the results.


 
Looping
Here is an example of for loop in JShell..simialrly we can use other loops.

jshell for loop
 
Functions
We can define functions and then execute them for the provided parameters.

jshell factorial function
 
Listing variables, methods, imports, source
We can list the variables, methods, import statements or sources as shown below. Note that, by default some import statements are added.

jshell listing
 
JShell also has the possibility of loading the statements from a file or to save the statements to a file.

And JShell also has a very helpful tab-completion, automatic addition of needed terminal semicolons, and configurable predefined imports and definitions.

JShell Advantages

  • Learning Java

    The immediate feedback is useful for new developers learning Java language in comparison to creating a test class with main method and adding System.out.println to check the values.

  • Rapid prototyping

    JShell facilitates prototyping and exploration of coding options with immediate results and feedback. For any changes to the classes, we can quickly update the cladd definitions, reset and try it again.

© 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags