In the previous section, we have created Java Hello World program and learn how to compile and run a Java program. In this section, we are going to learn, what happens while we compile and run the Java program. Moreover, we will see some questions based on the first program.
What happens at compile time?
At compile time, the Java file is compiled by Java Compiler (It does not interact with OS) and converts the Java code into bytecode.
What happens at runtime?
At runtime, the following steps are performed:
Classloader: It is the subsystem of JVM that is used to load class files.
Bytecode Verifier: Checks the code fragments for illegal code that can violate access rights to objects.
Interpreter: Read bytecode stream then execute the instructions.
Q) Can you save a Java source file by another name than the class name?
Yes, if the class is not public. It is explained in the figure given below:
|To compile:||javac Hard.java|
|To execute:||java Simple|
Observe that, we have compiled the code with file name but running the program with class name. Therefore, we can save a Java program other than class name.
Q) Can you have multiple classes in a java source file?
Yes, like the figure given below illustrates:
How a Java program works internally?
java file is passed through the compiler, which then encodes the source code into a machine-independent encoding, known as Bytecode. The content of each class contained in the source file is stored in a separate ‘.