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

JShell ใน Java 9

JShell คืออะไร?

JShell เป็นฟีเจอร์ใหม่ใน Java 9 ให้ความสามารถ REPL (Read-Eval-Print-Loop) ทดสอบ java logic และ expressions ได้ทันทีโดยไม่ต้อง compile

เริ่มต้นใช้งาน JShell

Step 1: เปิด JShell

C:\Users\User>JShell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro
jshell>

Step 2: ดูคำสั่งช่วยเหลือ

jshell> /help
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [|-all|-start]      | list the source you have typed
| /edit                     | edit a source entry
| /drop                     | delete a source entry
| /save [-all|-history|-start] | Save snippet source to a file
| /open                     | open a file as source input
| /vars [|-all|-start]      | list declared variables and values
| /methods [|-all|-start]   | list declared methods and signatures
| /types [|-all|-start]     | list declared types
| /imports                  | list imported items
| /exit                     | exit jshell
| /env                      | view or change evaluation context
| /reset                    | reset jshell
| /reload                   | reset and replay history
| /history                  | history of what you have typed
| /set                      | set jshell configuration
| /?                        | get information about jshell
...

Step 3: ดู Imports เริ่มต้น

jshell> /imports
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*

Step 4: ทดสอบคำนวณ

jshell> 3+5
$1 ==> 8
jshell> 8-4
$2 ==> 4
jshell> 2*6
$3 ==> 12
jshell> 9%3
$4 ==> 0
jshell> 8/2
$5 ==> 4

Step 5: ออกจาก JShell

jshell> /exit
| Goodbye

สรุป

JShell เป็นเครื่องมืออันทรงพลังสำหรับ learning, prototyping, และ exploration ของ Java API โดยไม่ต้องสร้างโปรเจกต์เต็มรูปแบบ