JShell and source-file mode
What you will learn
JShell is an interactive tool for experimenting with Java expressions and declarations. Java source-file mode is a separate launcher feature that can run a source file without a separate explicit compilation command.
public class Main { public static void main(String[] args) { System.out.println(6 * 7); }}Expected output:
42This uses the same arithmetic expression as the JShell experiment, but explicitly prints it.
How it works
Start jshell locally, enter 6 * 7, inspect the result, and exit with /exit. For a complete source file, java Main.java uses source-file mode on the chosen Java 21 lane. The class body below is ordinary Java and works in the explicit compile/run workflow as well. JShell prompt text is not part of a Java source file.
public class Main { public static void main(String[] args) { System.out.println((6 + 7) * 2); }}Expected output:
26Parentheses group the addition before multiplication.
Common mistakes
Do not paste JShell prompts such as jshell> or its displayed result labels into a .java file. They are interface text, not Java syntax.
Try it yourself
Compare entering 8 + 4 in JShell with printing 8 + 4 in a compiled class.
These are local Java practice tasks, not automatically graded ByteMind exercises. Use a patched JDK; no network or third-party dependency is needed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JShell and source-file mode — knowledge check
Check your understanding of jshell and source-file mode.
Which command exits JShell?
Which command uses source-file mode in this example?