Nearby lessons

96 of 125

Java - JShell

📌 What You Will Learn
  • JShell — the interactive Java playground
  • Modules (JPMS) — the big architecture change
  • Private methods in interfaces
  • Factory methods for collections: List.of, Set.of, Map.of
  • The updated try-with-resources

JShell is a core concept of the Java language. This lesson explains JShell — The Interactive Playground with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

JShell — The Interactive Playground

Before Java 9, to test one small line of Java you had to write a full class, save, compile and run. JShell removes all that — you type a line and get the answer instantly.

In simple words: JShell runs one line of Java at a time and prints the answer instantly — no class, no main() method, no compilation step, so it is the fastest way to test a small idea.
Trainer's Note: JShell is the best friend of a student. Use it to test any small idea — loops, methods, collections — without creating files. Start it by typing jshell in the command prompt.
Example01
JCode Cell
1C:\> jshell
2 
3jshell> int a = 10;
4jshell> int b = 20;
5jshell> a + b
6$3 ==> 30
7 
8jshell> "Java".toUpperCase()
9$4 ==> "JAVA"
10 
11jshell> /exit
📝 Key Takeaways
  • JShell lets you test Java interactively without files.
  • JPMS modules group packages with clear exports and requirements.
  • Interfaces can now have private helper methods.
  • List.of / Set.of / Map.of create short, immutable collections.
  • try-with-resources can use existing variables (Java 9).
  • New stream helpers: Optional.stream(), Stream.ofNullable().

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2