JShell 是一个交互式Java Shell工具,使我们能够从Shell中执行Java代码并立即显示输出。JShell是从命令行运行的REPL(读取评估打印循环)工具。我们可以通过简单地在命令提示符下键入“ jshell” 来启动JShell ,并使用“ / exit”命令退出jshell 。对于小片段,我们不需要在JShell中创建main()方法。
我们还可以使用此工具来实现主要集合,例如list,map 和set。在下面的程序中,我们可以在各种情况下实现ArrayList 。
C:\Users\User\Desktop\Java 9 QNA>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> ArrayList<String> list = new ArrayList<String>(); list ==> [] jshell> list.add("Jai");list.add("Adithya");list.add("Raja");list.add("Chaitanya"); $2 ==> true $3 ==> true $4 ==> true $5 ==> true jshell> list list ==> [Jai, Adithya, Raja, Chaitanya] jshell> list.isEmpty() $7 ==> false jshell> list.get(3) $8 ==> "Chaitanya" jshell> list.get(9) | java.lang.IndexOutOfBoundsException thrown: Index 9 out-of-bounds for length 4 | at Preconditions.outOfBounds (Preconditions.java:64) | at Preconditions.outOfBoundsCheckIndex (Preconditions.java:70) | at Preconditions.checkIndex (Preconditions.java:248) | at Objects.checkIndex (Objects.java:372) | at ArrayList.get (ArrayList.java:440) | at (#9:1) jshell> list.size() $10 ==> 4 jshell> if(list.isEmpty()) System.out.println("Empty"); else System.out.println("Not Empty"); Not Empty