Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Cách hiển thị các lệnh /list trong JShell Java 9

JShell (Java 9) là công cụ dòng lệnh cho phép nhập câu lệnh, biểu thức, phương thức, lớp không cần main(). Mỗi đoạn mã nhập vào được gán ID duy nhất (bắt đầu từ 1). Code tải sẵn khi khởi động có ID s1, s2... hoặc $1, $2.... Lệnh nội bộ /list liệt kê mã đã nhập.

Các biến thể /list:

/list
/list [ID]
/list [Code_Name]
/list -start
/list -all
  • /list: hiển thị code active session hiện tại (trừ code khởi động).
  • /list [ID]: hiển thị code theo ID.
  • /list [Code_Name]: hiển thị code theo tên.
  • /list -start: hiển thị code tải khi khởi động (import mặc định, script tùy chỉnh).
  • /list -all: liệt kê tất cả code (active, inactive, error, preloaded).

Ví dụ

jshell> /imports | import java.io.*; ... import java.util.stream.*;
jshell> LocalDate localDate = LocalDate.of(2020, 04, 10) | Error: cannot find symbol ...
jshell> int x = 10 x ==> 10
jshell> public enum Operate { ADDITION, DIVISION; } | created enum Operate
jshell> /list -all
s1 : import java.io.*; ... s10 : import java.util.stream.*;
e1 : LocalDate localDate = LocalDate.of(2020, 04, 10);
1 : int x = 10;
2 : public enum Operate { ADDITION, DIVISION; }

Có thể trùng tên biến, phương thức, kiểu. /list x hiển thị tất cả xuất hiện của tên x:

jshell> int x = 10 x ==> 10
jshell> public int x(int y) { return y; } | created method x(int)
jshell> public long x(long y, long z) { return y + z; } | created method x(long,long)
jshell> /list x
1 : int x = 10;
2 : public int x(int y) { return y; }
3 : public long x(long y, long z) { return y + z; }