JShell 是Java 9中引入的新的交互式命令行工具。该工具也可以称为REPL (Read-Eval-Print-Loop),因为它接受输入,评估输入并通过命令行将输出返回给用户。
我们可以使用与Java相同的JShell执行多行 控制流语句。诸如If-else语句,for循环 和while循环的控制流语句也可以在JShell中执行。它识别出多行语句是带有符号“…>”的提示,指示输入下一行语句。
jshell> int distance = 50 distance ==> 50 jshell> if(distance < 30) { ...> System.out.println("It's near"); ...> } else { ...> System.out.println("It's far"); ...> } It's far jshell>
jshell> int i = 10 i ==> 10 jshell> while(i < 25) { ...> System.out.println(i + " "); ...> i++; ...> } 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 jshell>
jshell> String names[] = {"Adithya", "Jai", "Raja", "Chaitanya", "Ravi", "Surya"} names ==> String[6] { "Adithya", "Jai", "Raja", "Chaitanya", "Ravi", "Surya" } jshell> for(String name : names) { ...> System.out.println(name); ...> } Adithya Jai Raja Chaitanya Ravi Surya jshell>