How Java works:
Create a source code document using java language with .java extension.
It compiles the source file and generate a class file writen in bytecode and also checks for the error in code.
The compiler generates a new class file, coded bytecode. Any device capable of running Java will be able to interpret/translate this class file and run it. The compiled bytecode is platform-independent.
Virtual machines :
All devices have a virtual Java machine (implemented in software) running inside their electronic gadgets.The virtual machine reads and runs the bytecode.
Source file is a file with .java extension that contains one or more class definitions. For now, we will be using source files that contain only one class.
Moreover, Java source file is called a compilation unit.
Let’s us see how to run a java program
/*This is a simple Java program.
Call this file "MyFirstCode.java".*/
To run this java code we need to first compile it.
To compile the MyFirstCode program, run the following command in command line as shown below.
The javac is a java compiler which compiles the java file and generate a class file called MyFirstCode.class that contains the bytecode version of the program.
To actually run the program, we must use the Java application launcher called java.
To execute the class file, pass the class name MyFirstCode as a command-line argument, as shown below:
On executing the above discussed program will give following output:
Mainly we will be working with command line interpretation. The benefit of using command line in beginner stage is that you will get to know about different error.
As we will get handy we will shift to IDE’s like netbeans ,eclipse. So, In the later sections we will briefly introduce you to these IDE’s.
What happens during the runtime ?
During runtime , following steps are performed.
Class file:The Java compiler compiles the source file with .java extension and generates a class file writen in platform-independent bytecode.
Class loader:Class loader is subsystems of jvm. They are responsible for loading Java classes during runtime dynamically to the JVM (Java Virtual Machine). It is also part of the JRE (Java Runtime Environment).
Bytecode verified:Checks the code fragments for illegal code that can violate access right to objects.Here the compiler will check for errors like Syntax errors,etc. If error exists, the execution will stop.
Interpreter:An interpreter is a program that reads in as input a source program, along with data for the program, and translates the source program instruction by instruction. Here in java , it reads bytecode stream then execute the instructions.
Device:The device start to work according to the instruction given by the code. All devices have a java virtual machine(implemented in software) running inside their electronic gadgets.The virtual machine reads and runs the bytecode.