본문 바로가기

ComputerScience/Java

[Java] Compile Time과 Runtime의 차이

이미지 출처: https://www.console.webcodein.com/wp-content/uploads/2019/11/maxresdefault.jpg

자바에서는 컴파일 타임과 런타임은 각각 프로그램의 실행의 두 단계를 나타냅니다.

컴파일 타임(Compile Time)

컴파일 타임은 사람이 작성한 소스 코드가 컴퓨터가 이해할 수 있는 기계어로 번역되는 단계입니다.
자바의 컴파일러는 이 단계에서 문법적 에러를 확인합니다. (e.g., missing semicolons, typos, incorrect syntax)
만약 어떤 문제도 발생하지 않으면, 자바 컴파일러(e.g., javac)는 bytecode를 생성합니다.

bytecode는 기계어는 아니지만, JVM(Java Virtual Machine)에 의해 해석될 수 있습니다.

 

또한 아래와 같은 일들이 수행됩니다.

  • 데이터 유형 호환성 확인
  • 클래스 및 방법이 올바르게 선언되었는지 확인

런타임(Runtime)

런타임은 컴파일된 코드(bytecode)가 JVM(Java Virtual Machine)에 의해 실행되는 단계입니다.

다음과 같은 순서로 실행됩니다.

  • JVM은 메모리로 bytecode를 로드합니다.
  • JVM은 bytecode를 해석하고 명령어를 순차적으로 실행합니다.
  • 실행하는 동안에 runtime errors를 발생시킬 수 있습니다. (e.g., OutOfInDex, division by zero)

이 단계에서는 다음과 같은 일이 발생합니다.

  • 객체를 메모리에 할당합니다.
  • 연산을 수행합니다.
  • 운영체제와 외부 리소스와 상호작용합니다.

요약

Feature Compile Time Runtime
Definition Source code is translated into machine code. Machine code is interpreted and executed.
What happens Syntax and code structure are checked; bytecode is generated. Bytecode instructions are interpreted and executed.
Tools involved Java compiler (e.g., javac) Java Virtual Machine (JVM)
Actions performed Checking for syntax errors, type compatibility, etc. Executing code, allocating memory, interacting with resources
Error handling Catches syntax errors Catches runtime errors (e.g., division by zero)

Key points to remember

  • 컴파일 타임에서는 프로그램이 실행되기 전 한 번만 발생합니다. Compile time happens once before the program is executed.
  • 런타임은 프로그램이 실행될 때마다 계속해서 발생합니다. (코드 한줄한줄의 명령어를 실행할 때라고 생각하면 됩니다.) Runtime happens every time the program is run.
  • 런타임 오류는 컴파일 시간 오류보다 진단 및 수정이 더 어려운 경우가 많습니다.

참고

  • Google Gemini