Profile Photo

Java internal working

Created on: Sep 19, 2024

java internal working Let's understand java internal working with hello world program. We will looks what happens internally behind the program.

Let's create a file Test.javaand write below program.

public class Test { public static void main(String[] args) { System.out.println("Hello world"); } }

java code can't be directed executed, it has to be compiled into bytecode. This will produce a file with .class extension which is bytecode. bytecode is a middle code between human-readable code and machine code. It is platform-independent and is saved in a .class file.

javac Test.java

Let's run the code.

java Test

For below code,below process happens

  1. Class Loading: Java Virtual Machine (JVM) loads Test.class code in memory which is primarily RAM.
  2. Bytecode Verification: JVM performs bytecode verification process to ensure the bytecode does not violate any security constraints. It checks for issues like stack overflows or illegal data access, memory corruption, illegal instructions
  3. Conversion: Just-In-Time(JIT) which is a part of JVM translates the bytecode into machine code that can be directly executed by the host machine.
  4. Execution: JVM looks for main() method, and starts executing the code.
  5. After execution, JVM terminates.