Home > Java > javaTutorial > body text

What are the different states of REPL in Java 9?

PHPz
Release: 2023-08-18 16:29:17
forward
845 people have browsed it

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");
         }
      }
   }
}
Copy after login

Output

<strong>Successfully executed
Successfully executed
Successfully executed  </strong>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!