|
| 1 | +== jshell: Java's Secret Weapon |
| 2 | + |
| 3 | +We are not going to start like all the other Java textbooks in the world, we're going to use `jshell`. |
| 4 | + |
| 5 | +It's one of Java's secret weapons for learning how to code. |
| 6 | + |
| 7 | +When you're learning to code, one of the biggest frustrations is the gap between having an idea and being able to test it. |
| 8 | +In many programming languages, 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. |
| 9 | + |
| 10 | +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. |
| 11 | +Think of it as having a conversation with the Java language itself. |
| 12 | + |
| 13 | +=== Why jshell is Revolutionary for Learning |
| 14 | + |
| 15 | +==== Immediate Feedback |
| 16 | + |
| 17 | +Traditional Java programming requires this cycle: |
| 18 | + |
| 19 | +``` |
| 20 | +Write code → Save to file → Compile → Run → See results |
| 21 | +``` |
| 22 | + |
| 23 | +With jshell, it's simply: |
| 24 | + |
| 25 | +``` |
| 26 | +Type code → See results immediately |
| 27 | +``` |
| 28 | + |
| 29 | +This instant feedback loop is **crucial** for learning because: |
| 30 | +- You can test ideas as soon as you think of them |
| 31 | +- You see results immediately, which reinforces learning |
| 32 | +- You can experiment without fear of "breaking" anything |
| 33 | +- You can try variations of code quickly |
| 34 | + |
| 35 | +==== No Ceremony, Just Code |
| 36 | + |
| 37 | +In traditional Java, even the simplest program requires a lot of "ceremony": |
| 38 | + |
| 39 | +[source] |
| 40 | +---- |
| 41 | +public class HelloWorld { |
| 42 | + public static void main(String[] args) { |
| 43 | + System.out.println("Hello, World!"); |
| 44 | + } |
| 45 | +} |
| 46 | +---- |
| 47 | + |
| 48 | +In jshell, you just type: |
| 49 | + |
| 50 | +[source] |
| 51 | +---- |
| 52 | +jshell> System.out.println("Hello, World!"); |
| 53 | +Hello, World! |
| 54 | +---- |
| 55 | + |
| 56 | +All that extra code (the class definition, main method, etc.) is handled automatically by jshell. |
| 57 | +You can focus on learning Java concepts without getting bogged down in syntax you don't understand yet. |
| 58 | + |
| 59 | +=== Getting Started with jshell |
| 60 | + |
| 61 | +==== Starting jshell |
| 62 | + |
| 63 | +Open your terminal or command prompt and type: |
| 64 | + |
| 65 | +[source] |
| 66 | +---- |
| 67 | +jshell |
| 68 | +---- |
| 69 | + |
| 70 | +You'll see something like: |
| 71 | + |
| 72 | +[source] |
| 73 | +---- |
| 74 | +| Welcome to JShell -- Version 17.0.2 |
| 75 | +| For an introduction type: /help intro |
| 76 | +
|
| 77 | +jshell> |
| 78 | +---- |
| 79 | + |
| 80 | +The `jshell>` prompt means you're ready to start coding! |
| 81 | + |
| 82 | +==== Your First Commands |
| 83 | + |
| 84 | +Let's start with some simple math: |
| 85 | + |
| 86 | +[source] |
| 87 | +---- |
| 88 | +jshell> 2 + 3 |
| 89 | +$1 ==> 5 |
| 90 | +
|
| 91 | +jshell> 10 * 7 |
| 92 | +$2 ==> 70 |
| 93 | +
|
| 94 | +jshell> 100 / 4 |
| 95 | +$3 ==> 25 |
| 96 | +---- |
| 97 | + |
| 98 | +Notice how jshell automatically creates variables (`$1`, `$2`, `$3`) to store your results. |
| 99 | +You can use these variables in later expressions: |
| 100 | + |
| 101 | +[source] |
| 102 | +---- |
| 103 | +jshell> $1 + $2 |
| 104 | +$4 ==> 75 |
| 105 | +---- |
| 106 | + |
| 107 | +==== Variables and Immediate Results |
| 108 | + |
| 109 | +Creating variables is effortless: |
| 110 | + |
| 111 | +[source] |
| 112 | +---- |
| 113 | +jshell> int age = 25 |
| 114 | +age ==> 25 |
| 115 | +
|
| 116 | +jshell> String name = "Alex" |
| 117 | +name ==> "Alex" |
| 118 | +
|
| 119 | +jshell> boolean isStudent = true |
| 120 | +isStudent ==> true |
| 121 | +---- |
| 122 | + |
| 123 | +jshell shows you the value that was assigned with the `==>` arrow. |
| 124 | +You can immediately use these variables: |
| 125 | + |
| 126 | +[source] |
| 127 | +---- |
| 128 | +jshell> "Hello, " + name |
| 129 | +$5 ==> "Hello, Alex" |
| 130 | +
|
| 131 | +jshell> age + 5 |
| 132 | +$6 ==> 30 |
| 133 | +
|
| 134 | +jshell> !isStudent |
| 135 | +$7 ==> false |
| 136 | +---- |
| 137 | + |
| 138 | +=== The Power of Experimentation |
| 139 | + |
| 140 | +==== Testing Code Snippets |
| 141 | + |
| 142 | +One of jshell's greatest strengths is letting you test small pieces of code before incorporating them into larger programs. |
| 143 | + |
| 144 | +Want to understand how string methods work? |
| 145 | + |
| 146 | +[source] |
| 147 | +---- |
| 148 | +jshell> String text = "Java Programming" |
| 149 | +text ==> "Java Programming" |
| 150 | +
|
| 151 | +jshell> text.length() |
| 152 | +$8 ==> 16 |
| 153 | +
|
| 154 | +jshell> text.toUpperCase() |
| 155 | +$9 ==> "JAVA PROGRAMMING" |
| 156 | +
|
| 157 | +jshell> text.substring(0, 4) |
| 158 | +$10 ==> "Java" |
| 159 | +
|
| 160 | +jshell> text.contains("Program") |
| 161 | +$11 ==> true |
| 162 | +---- |
| 163 | + |
| 164 | +Curious about arrays? |
| 165 | + |
| 166 | +[source] |
| 167 | +---- |
| 168 | +jshell> int[] numbers = {1, 2, 3, 4, 5} |
| 169 | +numbers ==> int[5] { 1, 2, 3, 4, 5 } |
| 170 | +
|
| 171 | +jshell> numbers[0] |
| 172 | +$12 ==> 1 |
| 173 | +
|
| 174 | +jshell> numbers.length |
| 175 | +$13 ==> 5 |
| 176 | +
|
| 177 | +jshell> numbers[2] = 10 |
| 178 | +$14 ==> 10 |
| 179 | +
|
| 180 | +jshell> numbers |
| 181 | +numbers ==> int[5] { 1, 2, 10, 4, 5 } |
| 182 | +---- |
| 183 | + |
| 184 | +==== Prototyping Methods |
| 185 | + |
| 186 | +You can even define and test methods instantly: |
| 187 | + |
| 188 | +[source] |
| 189 | +---- |
| 190 | +jshell> int square(int x) { return x * x; } |
| 191 | +| created method square(int) |
| 192 | +
|
| 193 | +jshell> square(5) |
| 194 | +$15 ==> 25 |
| 195 | +
|
| 196 | +jshell> square(7) |
| 197 | +$16 ==> 49 |
| 198 | +---- |
| 199 | + |
| 200 | +Want to improve your method? Just redefine it: |
| 201 | + |
| 202 | +[source] |
| 203 | +---- |
| 204 | +jshell> int square(int x) { |
| 205 | + ...> System.out.println("Squaring " + x); |
| 206 | + ...> return x * x; |
| 207 | + ...> } |
| 208 | +| modified method square(int) |
| 209 | +
|
| 210 | +jshell> square(3) |
| 211 | +Squaring 3 |
| 212 | +$17 ==> 9 |
| 213 | +---- |
| 214 | + |
| 215 | +=== Learning Through Discovery |
| 216 | + |
| 217 | +==== Exploring Object Methods |
| 218 | + |
| 219 | +jshell makes it easy to explore what you can do with Java objects. |
| 220 | +Use tab completion to discover methods: |
| 221 | + |
| 222 | +[source] |
| 223 | +---- |
| 224 | +jshell> String text = "Hello" |
| 225 | +text ==> "Hello" |
| 226 | +
|
| 227 | +jshell> text.<TAB> |
| 228 | +charAt( chars() codePointAt( codePointBefore( |
| 229 | +codePointCount( codePoints() compareTo( compareToIgnoreCase( |
| 230 | +concat( contains( contentEquals( endsWith( |
| 231 | +equals( equalsIgnoreCase( getBytes( |
| 232 | +getChars( hashCode() indexOf( intern() |
| 233 | +isEmpty() length() matches( offsetByCodePoints( |
| 234 | +regionMatches( repeat( replace( replaceAll( |
| 235 | +replaceFirst( split( startsWith( strip() |
| 236 | +stripLeading() stripTrailing() subSequence( substring( |
| 237 | +toCharArray() toLowerCase() toString() toUpperCase() |
| 238 | +trim() valueOf( |
| 239 | +---- |
| 240 | + |
| 241 | +This discovery process is invaluable for learning what's possible with Java. |
| 242 | + |
| 243 | +==== Understanding Error Messages |
| 244 | + |
| 245 | +jshell gives you immediate feedback when something goes wrong, helping you learn from mistakes: |
| 246 | + |
| 247 | +[source] |
| 248 | +---- |
| 249 | +jshell> int x = "hello" |
| 250 | +| Error: |
| 251 | +| incompatible types: java.lang.String cannot be converted to int |
| 252 | +| int x = "hello"; |
| 253 | +| ^-----^ |
| 254 | +
|
| 255 | +jshell> String name = |
| 256 | + ...> |
| 257 | + ...> System.out.println(name.length()) |
| 258 | +| Error: |
| 259 | +| variable name might not have been initialized |
| 260 | +| System.out.println(name.length()) |
| 261 | +| ^--^ |
| 262 | +---- |
| 263 | + |
| 264 | +These immediate error messages help you understand Java's type system and requirements. |
| 265 | + |
| 266 | +=== Useful jshell Commands |
| 267 | + |
| 268 | +jshell has built-in commands to help you work more efficiently: |
| 269 | + |
| 270 | +[source] |
| 271 | +---- |
| 272 | +jshell> /vars |
| 273 | +| int age = 25 |
| 274 | +| String name = "Alex" |
| 275 | +| boolean isStudent = true |
| 276 | +
|
| 277 | +jshell> /methods |
| 278 | +| int square(int) |
| 279 | +
|
| 280 | +jshell> /list |
| 281 | + 1 : int age = 25; |
| 282 | + 2 : String name = "Alex"; |
| 283 | + 3 : boolean isStudent = true; |
| 284 | +
|
| 285 | +jshell> /help |
| 286 | +| Type a Java language expression, statement, or declaration. |
| 287 | +| Or type one of the following commands: |
| 288 | +| /list [<name or id>|-all|-start] |
| 289 | +| list the source you have typed |
| 290 | +| /edit <name or id> |
| 291 | +| edit a source entry |
| 292 | +| /vars [<name or id>|-all|-start] |
| 293 | +| list the declared variables and their values |
| 294 | +| /methods [<name or id>|-all|-start] |
| 295 | +| list the declared methods and their signatures |
| 296 | +| /save [-all|-history|-start] <file> |
| 297 | +| Save snippet source to a file |
| 298 | +| /open <file> |
| 299 | +| open a file as source input |
| 300 | +| /exit [<integer-expression-snippet>] |
| 301 | +| exit the jshell tool |
| 302 | +
|
| 303 | +jshell> /exit |
| 304 | +---- |
| 305 | + |
| 306 | +=== Why This Matters for Learning |
| 307 | + |
| 308 | +==== Builds Confidence |
| 309 | + |
| 310 | +When you can immediately test ideas and see them work, you build confidence in your programming abilities. |
| 311 | +There's no long, intimidating compilation process - just immediate results. |
| 312 | + |
| 313 | +==== Encourages Experimentation |
| 314 | + |
| 315 | +jshell makes it safe to try things. |
| 316 | +If something doesn't work, you haven't "broken" anything. |
| 317 | +You just try again with a different approach. |
| 318 | + |
| 319 | +==== Reinforces Learning |
| 320 | + |
| 321 | +The immediate feedback loop means concepts stick better. |
| 322 | +When you see cause and effect immediately, you understand the relationship between code and results. |
| 323 | + |
| 324 | +==== Reduces Friction |
| 325 | + |
| 326 | +By eliminating the ceremony of full programs, jshell lets you focus on learning Java concepts rather than fighting with tools and syntax. |
| 327 | + |
| 328 | +=== Practical Learning Strategy |
| 329 | + |
| 330 | +Here's how to use jshell effectively while working through this book: |
| 331 | + |
| 332 | +1. **Start Every Session with jshell** - Before reading about a concept, try it in jshell |
| 333 | +2. **Test Every Example** - Don't just read the code examples; type them into jshell and see what happens |
| 334 | +3. **Experiment with Variations** - Once an example works, try changing it to see what happens |
| 335 | +4. **Use Tab Completion** - Explore methods and properties of objects by pressing Tab |
| 336 | +5. **Don't Fear Errors** - When something doesn't work, read the error message and learn from it |
| 337 | +6. **Save Interesting Code** - Use `/save` to keep code snippets you might want to reference later |
| 338 | + |
| 339 | +=== From jshell to Full Programs |
| 340 | + |
| 341 | +As you become more comfortable with Java, you'll naturally want to create full programs. |
| 342 | +jshell prepares you for this transition because: |
| 343 | + |
| 344 | +1. **You already know the Java syntax** - jshell uses real Java |
| 345 | +2. **You understand how Java objects work** - you've been experimenting with them |
| 346 | +3. **You're comfortable with Java's type system** - jshell has been teaching you about it |
| 347 | +4. **You know how to test code** - you can prototype in jshell before adding to full programs |
| 348 | + |
| 349 | +=== The Bottom Line |
| 350 | + |
| 351 | +jshell isn't just a tool - it's a philosophy of learning. |
| 352 | +It embodies the idea that programming should be interactive, experimental, and immediately rewarding. |
| 353 | + |
| 354 | +When you use jshell, you're not just learning Java syntax. |
| 355 | +You're developing a programmer's mindset: curious, experimental, and comfortable with iteration. |
| 356 | + |
| 357 | +So as you work through this book, remember: jshell is your coding playground. |
| 358 | +Use it freely, experiment boldly, and enjoy the immediate satisfaction of seeing your code come to life. |
| 359 | + |
| 360 | +This is how modern Java programming should be learned - with joy, curiosity, and immediate feedback. |
| 361 | + |
0 commit comments