Printing arrays like JShell does
println on an array prints its type and an identity code. Arrays.toString gives readable output, similar to JShell's display.
JShell is Java's interactive playground, included in every JDK since Java 9. You type one line of Java, press Enter, and see the result at once, with no class, no main method and no compile step, which makes it the fastest way to test an idea or explore a library method.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
In JShell, compute how many seconds are in a week. Then write the same as a program.
In JShell, define a method square(int n) and call it with 7 and -3. Then write a program with the same method.
Use JShell to find what " Java ".strip() and " Java ".length() return. Write a program that prints both.
Explain it without notes
What is a REPL, and how does JShell fit that description?
What do x ==> 5 and $2 ==> 10 mean in JShell output?
Name three ways JShell is more relaxed than a normal Java source file.
When would you use JShell, and when would you write a .java file instead?
Expected output
Before: [3, 1, 2]
After: [1, 2, 3]
Length: 3