AI Hints
Home›Java Tutorial›Installing the JDK, Trying Java in JShell and Your First Program
Java · Lesson 5 of 120

Trying Java in JShell

Try code instantly.

⏱ 2 min read▶ Video at 5:48🎓 Free
Watch this lesson in the Java courseWatch on YouTube ↗Full Java course

The JDK includes a tool called JShell. You type one line of Java, press enter, and see the answer straight away. It's perfect for experiments.

  • Comes with the JDK
  • Type a line
  • See the answer

Talking to Java

Type jshell, and press enter. JShell welcomes us, and shows its prompt. Two plus three gives five. JShell even names the result, dollar one. We create a variable x, holding seven, and then x times two gives fourteen. Asking for the length of the text hi, gives two. And slash exit leaves.

Terminal
jshell
|  Welcome to JShell -- Version 17.0.18
|  For an introduction type: /help intro

jshell> 2 + 3
$1 ==> 5

jshell> int x = 7
x ==> 7

jshell> x * 2
$3 ==> 14

jshell> "hi".length()
$4 ==> 2

jshell> /exit
|  Goodbye

JShell tips

In JShell, you don't need a class, or a main method, or even semicolons. The up arrow brings back earlier lines. Slash exit leaves. And nothing you type is saved, so real programs go in files.

keyvalue
No class neededjust type Java
Up arrowearlier lines
Leave/exit
Not saveduse files for programs

Summary

JShell runs one line of Java at a time, and shows the answer at once. Use it to experiment. Next, a real program in a file.

Key takeaways
  • jshell opens it
  • See results at once
  • /exit leaves
Next up: Your First Program — Main.java