๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

How Java Works  


Deep Explanation of How Java Works Step by Step

Below diagram visually explains the steps involved when writing, compiling, and executing a Java program using Notepad and CMD.
How Java Works

Below is detailed explanation:

  • Step 1: Writing the Java Program
    • We write our Java program in a plain text editor like Notepad.
    • The program is saved with a .java extension, e.g., MainApp.java.
      MainApp.java
      public class MainApp
      {
          public static void main(String[] args)
          {
              System.out.println("Hello Deepak");
          }
      }
    • The .java file contains our Java source code, including a class with a main method (the entry point of the program).
  • Step 2: Compilation
    • In compilation phase, we open CMD, navigate to the directory where our .java file is located, and run the javac command:
      javac MainApp.java
    • What happens in compilation phase:
      • The Java Compiler (javac) reads the .java file.
      • It checks the code for syntax errors.
      • If there are no errors, it compiles the code into bytecode, a platform-independent intermediate representation.
      • The bytecode is saved in a .class file (e.g., MainApp.class).
  • Step 3: Bytecode (.class file)
    • The .class file contains the compiled bytecode, which can be executed on any system with a Java Virtual Machine (JVM).
    • Bytecode ensures Java's "Write Once, Run Anywhere" principle because it is not tied to a specific machine.
  • Step 4: Execution
    • To run the program, we execute the java command in CMD: java MainApp
      Do not include the .class extension in this command.
    • What happens during execution phase:
      • JVM (Java Virtual Machine):
        • The JVM reads the bytecode in the .class file.
        • It converts the bytecode into machine code that the operating system understands.
        • It executes the machine code line by line.
      • The main method is the entry point for execution.
  • Step 5: Output
    • If the program contains a print statement, such as:
      System.out.println("Hello Deepak");
    • The JVM executes it, and the output is displayed in the Command Prompt:
      Hello Deepak