Java 9 REPL (JShell): 대화형 코딩 및 테스트 단순화
REPL은 Read-Eval-Print Loop의 약자입니다. JShell로 Java에 REPL 기능이 생겼습니다. REPL을 사용하면 javac로 컴파일하지 않고 Java 기반 로직을 코딩하고 테스트하며 계산 결과를 직접 볼 수 있습니다.
JShell 실행
명령 프롬프트를 열고 jshell 입력:
$ jshell
| Welcome to JShell -- Version 9-ea
| For an introduction type: /help intro
jshell>
JShell 명령어 보기
jshell 실행 후 /help 입력:
jshell> /help
| Type a Java language expression, statement, or declaration.
| Or type one of the following commands:
| /list [<name or id>|-all|-start] | list the source you have typed
| /edit <name or id> | edit a source entry referenced by name or id
| /drop <name or id> | delete a source entry referenced by name or id
| /save [-all|-history|-start] <file> | Save snippet source to a file.
| /open <file> | open a file as source input
| /vars [<name or id>|-all|-start] | list the declared variables and their values
| /methods [<name or id>|-all|-start] | list the declared methods and their signatures
| /types [<name or id>|-all|-start] | list the declared types
| /imports | list the imported items
JShell 명령어 실행
jshell 실행 후 /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.*
jshell>
JShell에서 계산 실행
JShell에서 간단한 계산 실행:
jshell> 3+1
$1 ==> 4
jshell> 13%7
$2 ==> 6
jshell> $2
$2 ==> 6
jshell>
JShell에서 함수 생성 및 사용
doubled() 함수 생성: int를 받아 2배 값 반환:
jshell> int doubled(int i){ return i*2;}
| created method doubled(int)
jshell> doubled(6)
$3 ==> 12
jshell>
JShell 종료
/exit 입력:
jshell> /exit
| Goodbye