Here, we will learn what happens while we compile and run the Java program.
Java at Compile time?
In Java, programs are not compiled into executable files,they are compiled into bytecode , then JVM (Java Virtual Machine) executes this at runtime. Java source code is compiled into bytecode when we use javac compiler. Bytecode is stored on disk with a file extension like .class.
At Runtime?
Runtime is the final phase of the program life-cycle in which the machine executes the program’s code and following steps are performed:
Now we will cover some point for interview purpose:
- we can save a java source file by other name than the class name. if the class is not public
class Basic{ public static void main(String args[]){ System.out.println("Hello World"); } }
In above program If we can save program using Test.java,
compile program using javac Test.java command and run using java Basic command then program will work correctly
Output
Hello World
- we can write multiple classes in a java program and below is the example
class Computer { void computerMethod() { System.out.println("Power Up! computer is working fine…"); } public static void main(String[] args) { Computer my = new Computer(); Laptop your = new Laptop();my.computerMethod();
your.laptopMethod();
} } class Laptop { void laptopMethod() { System.out.println("Power Up! laptop is working fine…"); } }
Output
Power Up! computer is working fine… Power Up! laptop is working fine…