{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreiagby32min4y4jjcxvpiqbnrrrosi6llwarz5wglgnsdviey6jqqq",
"uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3moruophka322"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreiajuw5oa5xbusayctlfqva2u65w4yxhdttve7xobcazecu6huvw64"
},
"mimeType": "image/webp",
"size": 62242
},
"path": "/jamilxt/jshell-javas-built-in-scratchpad-for-trying-code-fast-4dd4",
"publishedAt": "2026-06-21T07:08:44.000Z",
"site": "https://dev.to",
"tags": [
"java",
"productivity",
"tooling",
"tutorial",
"JShell: The Java Shell Tool"
],
"textContent": "Here's a familiar feeling. You want to test one small thing in Java — maybe how `String.substring()` behaves, or whether a regex pattern matches. So you create a file, write a class, add a `main` method, type `System.out.println`, compile, run. All that ceremony for a one-line answer.\n\nJShell does away with the ceremony. It's a read-eval-print loop (REPL) that ships with the JDK since Java 9. You type Java code, press Enter, and see the result immediately. No class, no `main` method, no compile step. Think of it as a calculator for Java.\n\n## Starting JShell\n\nOpen a terminal and type:\n\n\n\n jshell\n\n\nIf your JDK is installed and on your PATH, you'll see a welcome message and the `jshell>` prompt. That's it. You're in.\n\nIf you get a \"command not found\" error, double-check that the JDK (not just the JRE) is installed and the `bin` folder is on your PATH.\n\n## Your First Snippets\n\nIn JShell, the bits of code you type are called **snippets**. A snippet can be an expression, a variable, a method, or even a full class. Let's try a few:\n\n\n\n jshell> int age = 25\n age ==> 25\n\n jshell> age * 2\n $2 ==> 50\n\n jshell> \"hello\".toUpperCase()\n $3 ==> \"HELLO\"\n\n\nNotice two things. First, no semicolons needed. JShell adds them for you if you forget. Second, when you type an expression without storing it in a variable, JShell creates a **scratch variable** for you automatically (`$2`, `$3`, and so on). You can reuse these later:\n\n\n\n jshell> $2 + 10\n $5 ==> 60\n\n\nThis is handy when you're poking around and don't want to name every intermediate result.\n\n## Defining Methods and Classes\n\nYou can define full methods in JShell, not just expressions. Here's a simple one:\n\n\n\n jshell> int square(int n) {\n ...> return n * n;\n ...> }\n | created method square(int)\n\n jshell> square(7)\n $7 ==> 49\n\n\nThe `...>` prompt means JShell knows you're in the middle of a multi-line snippet and is waiting for you to finish. Close the braces and press Enter.\n\nClasses work the same way. You can define a class, instantiate it, and call methods on it, all without leaving the prompt.\n\n## Changing What You Already Wrote\n\nThis is where JShell gets genuinely useful. If you redefine a method or variable, the old version is replaced. No need to start over.\n\n\n\n jshell> String grade(int score) {\n ...> if (score >= 90) return \"Pass\";\n ...> return \"Fail\";\n ...> }\n | created method grade(int)\n\n jshell> grade(85)\n $3 ==> \"Fail\"\n\n\nThat pass threshold feels too strict. Just retype the method with a change:\n\n\n\n jshell> String grade(int score) {\n ...> if (score >= 80) return \"Pass\";\n ...> return \"Fail\";\n ...> }\n | modified method grade(int)\n\n jshell> grade(85)\n $5 ==> \"Pass\"\n\n\nThe method updated in place. Any other methods that called `grade` will use the new version automatically. For longer snippets, use the `/edit` command to pop open a text editor instead of retyping:\n\n\n\n jshell> /edit grade\n\n\nMake your changes, save, and close the editor. JShell picks up the new version.\n\n## The Slash Commands You'll Actually Use\n\nJShell has about 20 commands, all starting with a forward slash. You won't need all of them. Here are the ones that matter day to day:\n\n\n\n /list # show everything you've typed, with IDs\n /vars # show all variables and their values\n /methods # show all methods you've defined\n /types # show classes, interfaces, and enums\n /imports # show active imports\n\n\nTo manage your session:\n\n\n\n /save myfile.jsh # save all snippets to a file\n /open myfile.jsh # load snippets back in later\n /reset # wipe everything and start fresh\n /drop 3 # remove a specific snippet by ID\n /history # see everything typed, in order\n /exit # leave JShell\n\n\nPro tip: command abbreviations work as long as they're unique. `/l` runs `/list`. `/v` runs `/vars`. `/sa` runs `/save` (since `/s` alone is ambiguous between `/save` and `/set`).\n\n## Tab Completion and Shortcuts\n\nPress Tab while typing and JShell fills in the rest. This works for commands, class names, and method names. If there's more than one option, Tab shows you all of them.\n\nA few shortcuts worth knowing:\n\n * **Tab after a method's opening parenthesis** shows you the parameter types. Press Tab again for a description.\n * **Up/Down arrows** scroll through your history, just like a regular shell.\n * **Ctrl+R** searches backward through what you've typed.\n * **`/!`** reruns your last snippet. **`/-3`** reruns the snippet from 3 steps ago.\n\n\n\nThe `Shift+Tab` then `V` shortcut is a neat trick: type an expression, hit that combo, and JShell converts it into a variable declaration with the correct type filled in.\n\n## Feedback Modes\n\nBy default, JShell shows a reasonable amount of feedback. But when you're learning, more detail helps. Switch to verbose mode:\n\n\n\n jshell> /set feedback verbose\n\n\nVerbose mode explains what happened after each snippet — what was created, modified, or dropped, and what type it is. Once you're comfortable, switch back to normal or concise:\n\n\n\n jshell> /set feedback normal\n jshell> /set feedback concise\n\n\nThere's also `silent` mode if you want zero output (useful when piping JShell into scripts).\n\n## Saving and Reusing Sessions\n\nOne complaint people have about REPLs: you lose everything when you close them. JShell solves this with `/save` and `/open`.\n\nBefore you exit, save your work:\n\n\n\n /save practice.jsh\n\n\nNext time you start JShell, load it back:\n\n\n\n /open practice.jsh\n\n\nYou can also pass a file directly when launching:\n\n\n\n jshell practice.jsh\n\n\nThis makes JShell practical for more than throwaway experiments. Keep a `.jsh` file of helper methods you use often, and load it whenever you start a session.\n\n## When to Reach for JShell\n\nJShell isn't a replacement for your IDE. It's a tool for specific moments:\n\n * **Learning the language.** Type a construct, see what it does, no friction.\n * **Testing an API.** Wondering what `LocalDate.of(2026, 2, 31)` does? Find out in two seconds instead of writing a test class.\n * **Debugging a tricky expression.** Isolate the line that's misbehaving and poke at it in isolation.\n * **Prototyping.** Sketch out a method, iterate on it interactively, then paste the working version into your project.\n\n\n\nThe thing I like most about JShell is that it removes the fear of experimentation. In a normal Java project, trying something feels expensive. In JShell, it costs nothing. You type, you see, you move on.\n\n## Quick Summary\n\n * JShell is a REPL that ships with the JDK. Type `jshell` to start it.\n * Snippets can be expressions, variables, methods, or classes. No `main` method required.\n * Forgot a semicolon? JShell adds it for you.\n * Scratch variables (`$1`, `$2`, ...) hold results you didn't name.\n * Redefine a method to change it. Other code picks up the new version.\n * `/list`, `/vars`, `/methods`, `/save`, `/open`, `/reset`, and `/exit` cover most of what you need.\n * Tab completion works for commands and code. `/!` reruns your last snippet.\n * Use `/set feedback verbose` while learning, then dial it back.\n * Save sessions with `/save` and reload them with `/open`.\n\n\n\nBased on dev.java/learn — JShell: The Java Shell Tool",
"title": "JShell: Java's Built-In Scratchpad for Trying Code Fast"
}