Computer >> 컴퓨터 >  >> 프로그래밍 >> Java

Java 9의 JShell 완벽 가이드 – 컴파일 없이 코드를 즉시 테스트하는 REPL 도구

JShell이란 무엇인가?

JShell은 Java 9 버전에서 새롭게 도입된 개념으로, Java에 REPL(Read-Eval-Print-Loop) 기능을 제공합니다. JShell을 활용하면 별도의 컴파일 과정 없이 Java 기반 로직표현식(expression)을 즉시 실행하고 결과를 확인할 수 있습니다.

REPL은 입력한 코드에 대한 즉각적인 피드백 루프 역할을 하며, 코드를 빠르게 실험하고 학습할 수 있어 생산성 향상에 큰 도움이 됩니다. 특히 간단한 문법 확인, API 동작 검증, 알고리즘 프로토타이핑 등에 유용하게 사용됩니다.

1단계: JShell 실행하기

명령 프롬프트(Command Prompt)를 열고 JShell을 입력하면 됩니다.

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

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

jshell>

2단계: /help 명령어로 사용 가능한 명령 확인하기

JShell이 정상적으로 실행되면 /help 명령어를 입력하여 JShell에서 사용할 수 있는 명령어 목록을 확인할 수 있습니다.

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 referenced by name or id
| /drop
| delete a source entry referenced by name or id
| /save [-all|-history|-start]
| Save snippet source to a file.
| /open
| open a file as source input
| /vars [|-all|-start]
| list the declared variables and their values
| /methods [|-all|-start]
| list the declared methods and their signatures
| /types [|-all|-start]
| list the declared types
| /imports
| list the imported items
| /exit
| exit jshell
| /env [-class-path ] [-module-path ] [-add-modules <
| view or change the evaluation context
| /reset [-class-path ] [-module-path ] [-add-modules
| reset jshell
| /reload [-restore] [-quiet] [-class-path ] [-module-path
| reset and replay relevant history -- current or previous (
| /history
| history of what you have typed
| /help [|]
| get information about jshell
| /set editor|start|feedback|mode|prompt|truncation|format ...
| set jshell configuration information
| /? [|]
| get information about jshell
| /!
| re-run last snippet
| /
| re-run snippet by id
| /-
| re-run n-th previous snippet
|
| For more information type '/help' followed by the name of a
| command or a subject.
| For example '/help /list' or '/help intro'.
|
| Subjects:
|
| intro
| an introduction to the jshell tool
| shortcuts
| a description of keystrokes for snippet and command comple
| information access, and automatic code generation
| context
| the evaluation context options for /env /reload and /reset

3단계: /imports 명령어로 기본 임포트된 패키지 확인하기

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.*

덕분에 java.util.Listjava.io.File 같은 클래스를 별도의 import 문 없이 바로 사용할 수 있습니다.

4단계: 산술 연산 직접 실행해 보기

JShell의 핵심 장점은 코드를 즉시 평가한다는 점입니다. 간단한 산술 연산을 입력하면 결과가 바로 출력됩니다.

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

결과 앞의 $1, $2는 JShell이 자동으로 부여하는 변수명입니다. 이처럼 결과가 암묵적 변수에 저장되므로, 이후 계산에서 그 값을 다시 활용할 수도 있습니다.

5단계: JShell 종료하기

JShell 사용을 마치려면 /exit 명령어를 입력하면 됩니다.

jshell> /exit
| Goodbye

마무리

JShell은 Java 학습자에게는 문법을 손쉽게 익힐 수 있는 놀이터가 되고, 숙련된 개발자에게는 아이디어를 빠르게 검증할 수 있는 강력한 도구가 됩니다. Java 9 이상 버전이 설치되어 있다면 지금 바로 터미널에서 jshell을 입력해 REPL 환경의 편리함을 경험해 보시기 바랍니다.