Table of Content
What happens at compile time?
- Step 1:
- Write Source Code
- Step 2:
- Compile (Translate) Source Code into Machine Code
- Step 3:
- Execute(Run) Machine Code.
Program source code is converted during compile time into machine-readable bytecode (for Java) or straight into machine code (for languages like C or C++). There are multiple steps in this process:
- Lexical Analysis,also known as tokenization, divides the source code into discrete elements including operators, punctuation marks, identifiers, and keywords.
- Syntax Analysis (Parsing): The program's syntactic structure is represented by an abstract syntax tree (AST), which is created by arranging the tokens in accordance with the grammar rules of the language.
- Semantic Analysis: Using language-specific principles like type checking and scope resolution, the compiler verifies the code's semantic validity. Compile-time errors are reported to the programmer in the event of any faults or inconsistencies.
- Intermediate Code Generation: The compiler produces bytecode instructions that reflect the logic of the program for languages like Java that employ an intermediate representation like bytecode.
- Optimization (Optional): Some compilers apply optimization strategies, such eliminating pointless instructions, rearranging code for optimal performance, or inlining routines, to increase the effectiveness of the resulting code.
- Code Generation: The compiler next converts the intermediate representation into machine code or bytecode so that the target platform—the processor in the case of native machine code, or the JVM for Java bytecode—can execute it.
The compiler checks that the code follows the syntax and semantics of the language during this process, identifying any mistakes or inconsistencies early on. Should any problems be discovered, the compiler generates error messages that specify the issue's position and type, giving the programmer the opportunity to fix them before executing the program. Because they arise prior to the program being executed, compile-time mistakes are therefore frequently thought to be simpler to debug than runtime problems.
JVM, JRE, and JDK
JVM, JRE, and JDK are essential parts of the Java ecosystem that have distinct functions in the creation and operation of Java programs.
JVM (Java Virtual Machine):
An essential component of the Java platform is the JVM. It is an abstract computing device that offers a runtime environment for the execution of Java bytecode. The JVM's main purposes are as follows:
- Bytecode Execution: Java bytecode is the compiled form of Java source code, and it is interpreted and executed by the Java Virtual Machine (JVM).
- Memory Management: The Java Virtual Machine (JVM) controls how Java objects are allocated and deallocated. It also performs garbage collection to free up memory that has been taken up by inaccessible objects.
- Platform Independence: Java programs run on any platform thanks to the JVM's abstraction of the underlying operating system and hardware. Any platform or device that has a JVM implementation that is compatible can run Java bytecode.
JRE (Java Runtime Environment):
The Java Runtime Environment, or JRE, is a software package that comes with the Java Virtual Machine (JVM), Java class libraries, and other supporting files needed to run Java programs. Without the requirement for development tools, it offers the runtime environment needed to execute Java programs. The JRE comprises:
- JVM: The main Java Virtual Machine that runs Java bytecode.
- Java Class Libraries: A collection of pre-written Java classes and packages that offer networking, data structures, I/O operations, and GUI elements—all necessary for Java applications.
- Runtime Configuration Files: These are configuration files that specify how the Java Runtime Environment (JRE) behaves during runtime, including system settings, security rules, and environment variables.
JDK (Java Development Kit):
The Java Development Kit, or JDK, is a complete software development kit that is used to create Java applications. It comes with the JRE as well as the libraries, documentation, and development tools required for Java development. The JDK is made up of:
- JRE: The runtime environment needed to run Java programs is provided by JRE, or the Java Runtime Environment.
- Development Tools:Programs for developing, compiling, and debugging Java code, such as the Java compiler (javac), debugger (jdb), and other tools.
- Java Class Libraries: To create Java applications, developers can utilize the same collection of class libraries that are part of the JRE.
- Documentation: Java developers' lessons, guidelines, and API documentation.
- Code Samples: Examples and code samples to aid developers in learning and comprehending Java programming principles.
To put it briefly, the JRE offers the runtime environment for executing Java applications, the JVM is in charge of processing Java bytecode, and the JDK is a comprehensive development kit that has all the components required to create and run Java programs.

