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

สถานะ (Status States) ของ REPL ใน Java 9 มีกี่แบบ

REPL (Read-Evaluate-Print-Loop) มีสถานะสำหรับแต่ละ snippet กำหนดโดยผลลัพธ์ของเมธอด eval() ของ JShell instance

7 สถานะของ Snippet

  • VALID - snippet ถูกต้อง ทำงานสำเร็จ

  • DROPPED - snippet ไม่ active

  • NONEXISTENT - snippet ยังไม่มีอยู่จริง

  • OVERWRITTEN - snippet ถูกแทนที่ด้วย snippet ใหม่

  • RECOVERABLE_DEFINED - declaration snippet มี unresolved references ที่อาจแก้ไขได้

  • RECOVERABLE_NOT_DEFINED - declaration snippet มี unresolved references

  • REJECTED - compile error ไม่สามารถกลายเป็น valid ได้

ตัวอย่างตรวจสอบสถานะ

import java.util.List;
import jdk.jshell.*;
import jdk.jshell.Snippet.Status;

public class JShellStatusCheck {
    public static void main(String args[]) {
        JShell shell = JShell.create();
        List events = shell.eval(
            "int a, b, sum; " +
            "a = 12; b = 11; sum = a + b; " +
            "System.out.println(sum);"
        );
        
        for (SnippetEvent event : events) {
            Snippet snippet = event.snippet();
            Status status = shell.status(snippet);
            if (status == Status.VALID) {
                System.out.println("Successfully executed");
            }
        }
    }
}

ผลลัพธ์

Successfully executed
Successfully executed
Successfully executed