Java program compilation and execution

 Java Program Compilation and Execution

Because Java is a platform-independent programming language, it cannot be compiled in a single step. Instead, it uses a two-stage execution process, with the first phase taking place in an OS-independent compiler and the second in a virtual machine (JVM) that is custom-built for each operating system.


The following are the two major stages:


Compilation is the first principle.

The source '.java' file is first run via the compiler, which converts the source code into Bytecode, a machine-independent format. Each class in the source file has its own '.class' file containing its contents. The compiler performs the following steps when transforming source code to bytecode:


Step 1: Parse: Reads a collection of *.java source files and converts the token sequence into a string.

Step 2: Symbols for the definitions are entered into the symbol table.

Step 3: Process annotations: Processes annotations found in the given compilation units if they are requested.


Step 4: Attribute: Syntax trees' attributes. Name resolution, type checking, and continuous folding are all part of this process.


Phase 5: Flow: Analyzes the dataflow of the trees created in the previous step. This comprises assignments and reachability checks.


Step 6: Desugar: Rewrites the AST and removes some syntactic sugar in the process.


Step 7: Generate: This command creates.Class files.

Execution is the second principle.

The compiler generates class files that are independent of the machine or operating system, allowing them to run on any system. The main class file (the class that includes the function main) is handed to the JVM to run, and it then proceeds through three primary stages before reaching the final stage.

Some of the checks that were carried out were as follows:


Before a variable can be utilised, it must first be initialised.

The types of object references are matched by method calls.

The rules for gaining access to private data and techniques have not been broken.

Accesses to local variables are handled by the runtime stack.

The stack in the run-time environment does not overflow.

If any of the aforementioned checks fail, the verifier refuses to load the class.

Stage 3: Compiler for Just-In-Time


This is the java program's final stage, and it's job is to convert the loaded bytecode to machine code. Instead of having the JVM interpret the same sequence of bytecode repeatedly and paying the penalty of a rather slow translation process, a JIT compiler allows the hardware to execute native code.

C

The approach is well-illustrated by the diagram above, which shows how we arrived at our conclusion.

Conclusion: A java program is independent of the target operating system due to the two-step execution procedure discussed previously. However, as a result of this, the execution time is significantly longer than a comparable program written in a built platform-dependent language.

Implementation:

Consider a basic printing software that is stored in a machine's local directory.

Step 1: Create a file by putting some basic printing code in a text file and save it as a ".java" extension.





Step 2: Open the terminal (we're using macOS here) and navigate to the Desktop directory with the command below.

Step 3: Let's try compiling our software with the command below.

GFG.java is a java programme that runs on a computer.
Step 4: Finally, run it using the following command:



Comments

Popular posts from this blog

History of Java