Java Shell & REPL | no more public static void main

What is REPL?

REPL & Java Shell | no more public static void main

R(ead) E(val) P(rint) L(oop), is an interactive programming environment that allows you to enter code snippets or expressions and immediately see the results of their evaluation.

This makes it easy to experiment and test out ideas by scripts.

REPLs are commonly used for scripting languages like Python, and JavaScript, but they can be used for any language that supports interactive evaluation of code such as Java, Kotlin, Clojure.

REPL & Java Shell | no more public static void main

REPL in Java

With the release of Java 9, the Java development community was introduced to JShell, an official REPL tool that provides a streamlined and interactive way to write, test, and experiment with code snippets.

REPL & Java Shell | no more public static void main

How to run JShell?

To start JShell, enter the jshell command on the command line.

REPL & Java Shell | no more public static void main

Useful JShell commands

Enter the /help command on the command line to see details.

/list => list the source you have typed

jshell> /list 
 
   1 : import java.lang.String; 
   2 : "Hello world!".toUpperCase().split(" "); 
   3 : var a=5; 
   4 : String name = "gurkan";

/vars => list the declared variables and their values

jshell> /vars 
 
|    String[] $3 = String[2] { "HELLO", "WORLD!" } 
|    int a = 5 
|    String name = "gurkan"

/methods => list the declared methods and their signatures

jshell> int sum(int x,int y){ return x+y;} 
 
|  created method sum(int,int) 
 
jshell> sum(10,20); 
$9 ==> 30 
 
jshell> /methods 
|    int sum(int,int)

/imports => list the imported items

jshell> /imports 
 
|    import java.io.* 
|    import java.math.* 
|    import java.lang.String 
|    import java.util.*

/exit => to exit the jshell tool, ctrl+c won’t work!

jshell> /exit 
 
|    Goodbye

Examples

REPL & Java Shell | no more public static void main

Date-Time functions example

REPL & Java Shell | no more public static void main

Class example

REPL & Java Shell | no more public static void main

Streams example


Resources

https://www.digitalocean.com/community/tutorials/java-repl-jshell

https://www.tutorialspoint.com/how-to-implement-java-time-localdate-using-jshell-in-java-9

https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop

https://docs.oracle.com/javase/10/jshell/introduction-jshell.htm

https://www.infoworld.com/article/2971152/what-repl-means-for-java.html

My Github:

gurkanucar - Overview