Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

คำสั่งที่มีประโยชน์ใน JShell ของ Java 9

JShell Commands

JShell เป็น REPL tool ใน Java 9 ใช้ execute/test java code แบบ interactive ทำงานแบบ REPL (Read-Evaluate-Print-Loop)

คำสั่งสำคัญ

1. /vars - แสดงตัวแปรทั้งหมด

รวม implicit variables ($1, $2, ...) ที่ JShell สร้างให้

jshell> 2+5
$1 ==> 7
jshell> 8%3
$2 ==> 2
jshell> 9/3
$3 ==> 3
jshell> /vars
| int $1 = 7
| int $2 = 2
| int $3 = 3

2. /types [option] - แสดง Types (classes, interfaces, enums)

jshell> class Test1 { void testMethod1() { System.out.println("TutorialsPoint"); } }
| created class Test1
jshell> /types Test1
| class Test1
jshell> /types Test2
| No such snippet: Test2

3. /methods - แสดงเมธอดที่ประกาศแล้ว

jshell> String demo(String firstName, String lastName) { return firstName + lastName; }
| created method demo(String, String)
jshell> /methods
| String demo(String, String)

4. /list - แสดง Snippets ทั้งหมด (มี ID)

jshell> /list
1 : 2+5
2 : 8%3
3 : 9/3
4 : class Test1 { void testMethod1() { System.out.println("TutorialsPoint"); } }
5 : String demo(String firstName, String lastName) { return firstName + lastName; }

คำสั่งอื่นๆ ที่น่าสนใจ

  • /imports - แสดง imports
  • /save - บันทึก snippets
  • /open - โหลดไฟล์
  • /edit - แก้ไขใน editor
  • /drop <id> - ลบ snippet
  • /help - ช่วยเหลือ
  • /exit - ออกจาก JShell

สรุป

คำสั่งเหล่านี้ช่วยจัดการ session JShell ได้อย่างมีประสิทธิภาพ เหมาะสำหรับการ explore และ prototype