X Tutup
Skip to content

Latest commit

 

History

History
369 lines (268 loc) · 10.3 KB

File metadata and controls

369 lines (268 loc) · 10.3 KB

jshell: Java’s Secret Weapon

We are not going to start like all the other Java textbooks in the world, we’re going to use jshell.

It’s one of Java’s secret weapons for learning how to code.

When you’re learning to code, one of the biggest frustrations is the gap between having an idea and being able to test it. In many programming languages, like Java before 2017, you have to write a complete program, save it to a file, compile it, and then run it just to see if a simple line of code works.

Java changed all of that in 2017 with the introduction of jshell - an interactive programming environment that lets you write and test Java code instantly. Think of it as having a conversation with the Java language itself.

Now, this section is going to mention some things you have not been introduced to yet. Don’t let that bother you. jshell is a powerful thing, and we have to start somewhere, so just kind of move through these ideas as smoothly as you can.

Why jshell is Revolutionary for Learning

Immediate Feedback

Traditional Java programming requires this cycle:

Write code > Save to file > Compile > Run > See results

With jshell, it’s simply:

Type code > See results immediately

This instant feedback loop is crucial for learning because: - You can test ideas as soon as you think of them - You see results immediately, which reinforces learning - You can experiment without fear of "breaking" anything - You can try variations of code quickly

No Ceremony, Just Code

In traditional Java, even the simplest program requires a lot of "ceremony". In many languages, you can just write a line of code and see what happens. But Java requires you to wrap everything in a class and a main method:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

This can be intimidating for beginners who just want to see how a simple line of code works.

With jshell, you can skip all that boilerplate:

jshell> System.out.println("Hello, World!");
Hello, World!

All that extra code (the class definition, main method, etc.) is handled automatically by jshell. You can focus on learning Java concepts without getting bogged down in syntax you don’t understand yet.

And that is going make the following chapters much easier to understand, letting you learn the concepts without the extra noise.

Getting Started with jshell

Starting jshell

Open your terminal or command prompt and type:

jshell

You’ll see something like:

|  Welcome to JShell -- Version 17.0.2
|  For an introduction type: /help intro

jshell>

The jshell> prompt means you’re ready to start coding!

Your First Commands

Let’s start with some simple math:

jshell> 2 + 3
$1 ==> 5

jshell> 10 * 7
$2 ==> 70

jshell> 100 / 4
$3 ==> 25

Notice how jshell automatically creates variables ($1, $2, $3) to store your results. You can use these variables in later expressions:

jshell> $1 + $2
$4 ==> 75

Variables and Immediate Results

Creating variables is effortless:

jshell> int age = 25
age ==> 25

jshell> String name = "Alex"
name ==> "Alex"

jshell> boolean isStudent = true
isStudent ==> true

jshell shows you the value that was assigned with the =⇒ arrow. You can immediately use these variables:

jshell> "Hello, " + name
$5 ==> "Hello, Alex"

jshell> age + 5
$6 ==> 30

jshell> !isStudent
$7 ==> false

The Power of Experimentation

Testing Code Snippets

One of jshell’s greatest strengths is letting you test small pieces of code before incorporating them into larger programs.

Want to understand how string methods work?

jshell> String text = "Java Programming"
text ==> "Java Programming"

jshell> text.length()
$8 ==> 16

jshell> text.toUpperCase()
$9 ==> "JAVA PROGRAMMING"

jshell> text.substring(0, 4)
$10 ==> "Java"

jshell> text.contains("Program")
$11 ==> true

Curious about arrays?

jshell> int[] numbers = {1, 2, 3, 4, 5}
numbers ==> int[5] { 1, 2, 3, 4, 5 }

jshell> numbers[0]
$12 ==> 1

jshell> numbers.length
$13 ==> 5

jshell> numbers[2] = 10
$14 ==> 10

jshell> numbers
numbers ==> int[5] { 1, 2, 10, 4, 5 }

Defining Methods

You can even define and test methods instantly:

jshell> int square(int x) { return x * x; }
|  created method square(int)

jshell> square(5)
$15 ==> 25

jshell> square(7)
$16 ==> 49

Want to improve your method? Just redefine it:

jshell> int square(int x) {
   ...>     System.out.println("Squaring " + x);
   ...>     return x * x;
   ...> }
|  modified method square(int)

jshell> square(3)
Squaring 3
$17 ==> 9

Learning Through Discovery

Exploring Object Methods

jshell makes it easy to explore what you can do with Java objects. Each object has methods you can call to perform actions or retrieve information. The fact is that you can discover these methods interactively is a huge advantage. The methods are in fact part of the object’s class, but we will get to that much later. For now, just know that you can explore what methods are available on an object. Use tab completion to discover methods:

jshell> String text = "Hello"
text ==> "Hello"

jshell> text.<TAB>
charAt(        chars()        codePointAt(   codePointBefore(
codePointCount( codePoints()   compareTo(     compareToIgnoreCase(
concat(        contains(      contentEquals( endsWith(
equals(        equalsIgnoreCase(             getBytes(
getChars(      hashCode()     indexOf(       intern()
isEmpty()      length()       matches(       offsetByCodePoints(
regionMatches( repeat(        replace(       replaceAll(
replaceFirst(  split(         startsWith(    strip()
stripLeading() stripTrailing() subSequence(   substring(
toCharArray()  toLowerCase()  toString()     toUpperCase()
trim()         valueOf(

This discovery process is invaluable for learning what’s possible with Java.

Understanding Error Messages

jshell gives you immediate feedback when something goes wrong, helping you learn from mistakes:

jshell> int x = "hello"
|  Error:
|  incompatible types: java.lang.String cannot be converted to int
|  int x = "hello";
|          ^-----^

jshell> String name =
   ...>
   ...> System.out.println(name.length())
|  Error:
|  incompatible types: void cannot be converted to java.lang.String
|  System.out.println(name.length());
|  ^-------------------------------^

These immediate error messages help you understand Java’s type system and requirements.

Useful jshell Commands

jshell has built-in commands to help you work more efficiently:

jshell> /vars
|    int age = 25
|    String name = "Alex"
|    boolean isStudent = true

jshell> /methods
|    int square(int)

jshell> /list
 1 : int age = 25;
 2 : String name = "Alex";
 3 : boolean isStudent = true;

jshell> /help
|  Type a Java language expression, statement, or declaration.
|  Or type one of the following commands:
|  /list [<name or id>|-all|-start]
|       list the source you have typed
|  /edit <name or id>
|       edit a source entry
|  /vars [<name or id>|-all|-start]
|       list the declared variables and their values
|  /methods [<name or id>|-all|-start]
|       list the declared methods and their signatures
|  /save [-all|-history|-start] <file>
|       Save snippet source to a file
|  /open <file>
|       open a file as source input
|  /exit [<integer-expression-snippet>]
|       exit the jshell tool

jshell> /exit

(Your help output may vary based on your jshell version.)

Why This Matters for Learning

  • Builds Confidence When you can immediately test ideas and see them work, you build confidence in your programming abilities. There’s no long, intimidating compilation process - just immediate results.

  • Encourages Experimentation jshell makes it safe to try things. If something doesn’t work, you haven’t "broken" anything. You just try again with a different approach.

  • Reinforces Learning The immediate feedback loop means concepts stick better. When you see cause and effect immediately, you understand the relationship between code and results.

  • Reduces Friction By eliminating the complexity of full java programs, jshell lets you focus on learning Java concepts rather than fighting with tools and syntax.

Practical Learning Strategy

Here’s how to use jshell effectively while working through this book:

  1. Start Every Session with jshell - Before reading about a concept, try it in jshell

  2. Test Every Example - Don’t just read the code examples; type them into jshell and see what happens

  3. Experiment with Variations - Once an example works, try changing it to see what happens

  4. Use Tab Completion - Explore methods and properties of objects by pressing Tab

  5. Don’t Fear Errors - When something doesn’t work, read the error message and learn from it

  6. Save Interesting Code - Use /save to keep code snippets you might want to reference later

From jshell to Full Programs

As you become more comfortable with Java, you’ll naturally want to create full programs. jshell prepares you for this transition because:

  1. You already know the Java syntax - jshell uses real Java

  2. You understand how Java objects work - you’ve been experimenting with them

  3. You’re comfortable with Java’s type system - jshell has been teaching you about it

  4. You know how to test code - you can prototype in jshell before adding to full programs

The Bottom Line

jshell isn’t just a toy or simple tool - it’s a philosophy of learning. It embodies the idea that programming should be interactive, experimental, and immediately rewarding.

When you use jshell, you’re not just learning Java syntax. You’re developing a programmer’s mindset: curious, experimental, and comfortable with iteration.

Many Java programmers have learned Java without jshell and have struggled with the initial complexity of the language. But with jshell, you have a powerful ally that makes learning Java more accessible and enjoyable.

So as you work through this book, remember: jshell is your coding playground. Use it freely, experiment boldly, and enjoy the immediate satisfaction of seeing your code come to life.

This is how modern Java programming should be learned - with joy, curiosity, and immediate feedback.

X Tutup