Sub topic 12
Choosing a feedback mode
This chapter assumes you can already start a JShell session, enter expressions and declarations, and use slash commands such as /vars and /list. You need JDK 9 or later. The sample output was written for recent JDKs, so exact wording may differ slightly on yours.
What a feedback mode controls
Every time you enter a snippet, JShell reports what happened. It might print x ==> 5 for a variable, | created method square(int) for a method, or an error message. A feedback mode is a named set of rules for how much of that reporting appears and how the prompt looks.
A mode controls three things:
- Which events produce a message, such as creating, modifying, replacing or dropping a declaration.
- How much detail each message carries, such as the type of a new variable.
- The prompt text, and how long printed values may be before JShell truncates them.
A mode changes only the reporting. It never changes what your code does. Output your code produces itself, such as System.out.println, always appears.
The four built-in modes
JShell ships with four modes. The default is normal.
| Mode | Prompt | What it reports |
|---|---|---|
| `verbose` | `jshell> ` | Values plus a description of every change, such as the type of each new variable |
| `normal` | `jshell> ` | Values and messages for created, modified and replaced methods and classes |
| `concise` | `jshell> ` | Mostly expression values and errors. Declarations are usually silent |
| `silent` | `-> ` | Nothing, except errors and output from your own code |
Comparing the modes on the same input
Here is the same variable declaration in verbose mode. The extra line states the kind of thing created and its type.
jshell> /set feedback verbose
| Feedback mode: verbose
jshell> int x = 5
x ==> 5
| created variable x : intIn normal mode you get the value line and no description. Methods and classes still get a message.
jshell> /set feedback normal
| Feedback mode: normal
jshell> int y = 6
y ==> 6
jshell> int square(int n) { return n * n; }
| created method square(int)In concise mode, declarations typically print nothing. Bare expressions still show their value, because that is the point of evaluating them.
jshell> /set feedback concise
jshell> int z = 7
jshell> z * 2
$3 ==> 14In silent mode the prompt changes to -> and even expression values are suppressed.
jshell> /set feedback silent
-> int w = 8
-> w * 2
-> System.out.println(w * 2)
16The last line is output from your own code, so silent mode does not hide it. The w * 2 line printed nothing, so a bare expression has no visible effect in silent mode. If you want to see a value, print it explicitly.
Switching modes during a session
Run /set feedback with no argument to see the current mode and the list of available modes. Give it a mode name to switch.
jshell> /set feedback
| /set feedback verbose
|
| Available feedback modes:
| concise
| normal
| silent
| verbose
jshell> /set feedback conciseThe change applies to snippets you enter from then on. It does not alter variables or methods that already exist.
Choosing a mode at launch
You can pick a mode when you start JShell, which is useful in scripts and shell aliases.
jshell --feedback concise
jshell -q
jshell -s
jshell -vThe short options are -q for concise, -s for silent and -v for verbose. The --feedback option also accepts normal and any custom mode you have defined.
Making a choice permanent
A /set feedback command lasts only for the current session. Add -retain to store the choice in your JShell preferences, so later sessions start in that mode.
jshell> /set feedback -retain concise
| Feedback mode: conciseIf you retain a mode and later forget, the terse output of a new session can look like a bug. Run /set feedback with no argument to check the mode first.
Which mode for which job
Pick a mode based on what you are reading the output for.
verbosesuits learning. It confirms the type JShell inferred and tells you when a snippet replaced an earlier one.normalsuits everyday exploration and is a sensible default.concisesuits fast experiments where you already know what you declared and only want results.silentsuits running script files, where you control the output withSystem.out.printlnand do not want echoed values mixed in.
A script that loads in silent mode keeps its output clean. You can switch mode inside a script file by putting the slash command on a line of its own.
/set feedback silent
int total = 0;
for (int i = 1; i <= 4; i++) { total += i; }
System.out.println("total = " + total);
/set feedback normal
/exitRunning jshell demo.jsh prints only total = 10. The /set feedback normal line restores the usual reporting before the file ends.
Defining your own mode
A custom mode starts as a copy of an existing one, so you change only what you want. The -command flag tells JShell that the mode should show messages for slash commands as well.
jshell> /set mode mine normal -command
| Created new feedback mode: mine
jshell> /set prompt mine "mine> " " ...> "
jshell> /set truncation mine 40 expression,varvalue
jshell> /set feedback mine
| Feedback mode: mine
mine> The two quoted strings in /set prompt are the normal prompt and the continuation prompt, which appears when a snippet spans several lines. The /set truncation line limits printed values to 40 characters, for the selectors expression and varvalue. This helps when you display large collections.
To remove a mode you created, use /set mode mine -delete. You cannot delete the built-in modes, and you cannot delete the mode that is currently active.
Common errors
- Output seems missing. You are probably in
silentmode, or a retainedconcisemode. Run/set feedbackto check. Unknown feedback mode. The name is misspelled or the custom mode was never created in this session. Retained custom modes need the-retainflag when you create or change them.- Printed values are cut off. Truncation is applied per mode. Raise the limit with
/set truncationon a custom mode. - Expecting
--feedbackto change code behavior. It does not. It affects reporting only.
Exercise
You start jshell -s and enter int count = 3, then count + 1, then System.out.println(count + 1). Which of these lines produce visible output, and what single command makes the first two show feedback?
Show answer
Only System.out.println(count + 1) produces visible output, which is 4, because it is output from your own code. The declaration and the bare expression are silent in silent mode. Running /set feedback normal (or verbose) makes later declarations and expression values show feedback.
Exercise
Write the commands that create a mode named quick based on concise, give it the prompt q> with continuation prompt .. , make it the active mode, and keep it for future sessions.
Show answer
/set mode quick concise -command /set prompt quick "q> " ".. " /set feedback -retain quick