➜ ~cedar jshell | Welcome to JShell -- Version 11.0.9.1 | For an introduction type: /help intro
jshell>
退出方式稍微有一些特别,命令是 /exit
1 2
jshell> /exit | Goodbye
jshell -h 可以发现提供了几个选项,这仨比较有意思
1 2 3
-q Quiet feedback. Same as: --feedback concise -s Really quiet feedback. Same as: --feedback silent -v Verbose feedback. Same as: --feedback verbose
试了一下 -s 非常安静的反馈,看起来真的清爽
1 2 3
➜ ~cedar jshell -s -> int a = 1; -> int b = 2;
初学者还是别整这么安静了,使用 -v 开启详细反馈吧
1 2 3 4 5
➜ ~cedar jshell -v | Welcome to JShell -- Version 11.0.9.1 | For an introduction type: /help intro
jshell>
简单使用
变量赋值
赋几个值看看
1 2 3 4 5 6 7 8 9 10 11
jshell> int a = 1 a ==> 1 | created variable a : int
jshell> a + 1 $2 ==> 2 | created scratch variable $2 : int
jshell> $2 + a $3 ==> 3 | created scratch variable $3 : int
可见:没有指定变量的数字会自动赋值给临时变量,我们也可以使用这个临时变量
方法与类
那创建方法呢?
1 2 3 4
jshell> String addMark(Word word) { ...> return word.val + "!"; ...> } | created method addMark(Word), however, it cannot be referenced until class Word is declared
这里方法传入了一个不存在的类,他告诉我们要定义这个类才能使用这个方法,那定义一下吧
1 2 3 4 5 6 7 8 9
jshell> class Word { ...> String val; ...> public Word() { ...> val = "hello word"; ...> } ...> } | created class Word | update replaced method addMark(Word)
创建个对象调用一下
1 2 3 4 5 6 7 8
jshell> Word words = new Word() words ==> [email protected] | created variable words : Word