Home >Java >javaTutorial >What are the different states of REPL in Java 9?

What are the different states of REPL in Java 9?

PHPz
PHPzforward
2023-08-18 16:29:17930browse

REPL stands for Read-Evaluate-Print-Loop. It saves some state, one for every statement in JShell. This state determines the execution status of code fragments and variables. It can be determined by the result of the eval() method of the JShell instance, which is used to evaluate the code.

Seven different states are listed below.

  • DROPPED: The code snippet is inactive.
  • NONEXISTENT: The snippet is inactive because it does not exist yet.
  • OVERWRITTEN: The code snippet is inactive because it was replaced by a new code snippet.
  • RECOVERABLE_DEFINED: A code snippet is a declaration code fragment whose body may have unresolved references or other issues.
  • RECOVERABLE_NOT_DEFINED: A code snippet is a declaration code fragment whose body may have unresolved references or other issues.
  • REJECTED: The code snippet is inactive because compilation failed on initial evaluation and cannot be made valid by making further changes to the JShell state.
  • VALID: The code snippet is a valid code snippet.

Example

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

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

Output

<strong>Successfully executed
Successfully executed
Successfully executed  </strong>

The above is the detailed content of What are the different states of REPL in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete