{"slug": "jshell-java-s-built-in-scratchpad-for-trying-code-fast", "title": "JShell: Java's Built-In Scratchpad for Trying Code Fast", "summary": "JShell, a read-eval-print loop (REPL) included with the JDK since Java 9, allows developers to quickly test Java code snippets without the overhead of creating a class or main method. It supports expressions, variables, methods, and classes, with features like automatic semicolons, scratch variables, and in-place method redefinition. Key commands include /list, /vars, /methods, /save, and /open, enabling efficient experimentation and prototyping.", "body_md": "Here's a familiar feeling. You want to test one small thing in Java — maybe how `String.substring()`\n\nbehaves, or whether a regex pattern matches. So you create a file, write a class, add a `main`\n\nmethod, type `System.out.println`\n\n, 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`\n\nmethod, no compile step. Think of it as a calculator for Java.\n\nOpen a terminal and type:\n\n```\njshell\n```\n\nIf your JDK is installed and on your PATH, you'll see a welcome message and the `jshell>`\n\nprompt. 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`\n\nfolder is on your PATH.\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``` php\njshell> int age = 25\nage ==> 25\n\njshell> age * 2\n$2 ==> 50\n\njshell> \"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`\n\n, `$3`\n\n, and so on). You can reuse these later:\n\n``` php\njshell> $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\nYou can define full methods in JShell, not just expressions. Here's a simple one:\n\n```\njshell> int square(int n) {\n   ...>     return n * n;\n   ...> }\n|  created method square(int)\n\njshell> square(7)\n$7 ==> 49\n```\n\nThe `...>`\n\nprompt 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\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```\njshell> String grade(int score) {\n   ...>     if (score >= 90) return \"Pass\";\n   ...>     return \"Fail\";\n   ...> }\n|  created method grade(int)\n\njshell> grade(85)\n$3 ==> \"Fail\"\n```\n\nThat pass threshold feels too strict. Just retype the method with a change:\n\n```\njshell> String grade(int score) {\n   ...>     if (score >= 80) return \"Pass\";\n   ...>     return \"Fail\";\n   ...> }\n|  modified method grade(int)\n\njshell> grade(85)\n$5 ==> \"Pass\"\n```\n\nThe method updated in place. Any other methods that called `grade`\n\nwill use the new version automatically. For longer snippets, use the `/edit`\n\ncommand to pop open a text editor instead of retyping:\n\n```\njshell> /edit grade\n```\n\nMake your changes, save, and close the editor. JShell picks up the new version.\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/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/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`\n\nruns `/list`\n\n. `/v`\n\nruns `/vars`\n\n. `/sa`\n\nruns `/save`\n\n(since `/s`\n\nalone is ambiguous between `/save`\n\nand `/set`\n\n).\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`/!`\n\n`/-3`\n\nThe `Shift+Tab`\n\nthen `V`\n\nshortcut 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\nBy default, JShell shows a reasonable amount of feedback. But when you're learning, more detail helps. Switch to verbose mode:\n\n```\njshell> /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```\njshell> /set feedback normal\njshell> /set feedback concise\n```\n\nThere's also `silent`\n\nmode if you want zero output (useful when piping JShell into scripts).\n\nOne complaint people have about REPLs: you lose everything when you close them. JShell solves this with `/save`\n\nand `/open`\n\n.\n\nBefore you exit, save your work:\n\n```\n/save practice.jsh\n```\n\nNext time you start JShell, load it back:\n\n```\n/open practice.jsh\n```\n\nYou can also pass a file directly when launching:\n\n```\njshell practice.jsh\n```\n\nThis makes JShell practical for more than throwaway experiments. Keep a `.jsh`\n\nfile of helper methods you use often, and load it whenever you start a session.\n\nJShell isn't a replacement for your IDE. It's a tool for specific moments:\n\n`LocalDate.of(2026, 2, 31)`\n\ndoes? Find out in two seconds instead of writing a test class.The 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`jshell`\n\nto start it.`main`\n\nmethod required.`$1`\n\n, `$2`\n\n, ...) hold results you didn't name.`/list`\n\n, `/vars`\n\n, `/methods`\n\n, `/save`\n\n, `/open`\n\n, `/reset`\n\n, and `/exit`\n\ncover most of what you need.`/!`\n\nreruns your last snippet.`/set feedback verbose`\n\nwhile learning, then dial it back.`/save`\n\nand reload them with `/open`\n\n.Based on dev.java/learn — [JShell: The Java Shell Tool](https://dev.java/learn/jshell-tool/)", "url": "https://wpnews.pro/news/jshell-java-s-built-in-scratchpad-for-trying-code-fast", "canonical_source": "https://dev.to/jamilxt/jshell-javas-built-in-scratchpad-for-trying-code-fast-4dd4", "published_at": "2026-06-21 07:08:44+00:00", "updated_at": "2026-06-21 08:06:47.298084+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["JShell", "JDK", "Java"], "alternates": {"html": "https://wpnews.pro/news/jshell-java-s-built-in-scratchpad-for-trying-code-fast", "markdown": "https://wpnews.pro/news/jshell-java-s-built-in-scratchpad-for-trying-code-fast.md", "text": "https://wpnews.pro/news/jshell-java-s-built-in-scratchpad-for-trying-code-fast.txt", "jsonld": "https://wpnews.pro/news/jshell-java-s-built-in-scratchpad-for-trying-code-fast.jsonld"}}