Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to Create, Compile and Execute a Java program

Introduction : -
Java is extensively used programming language. It is been loved by many programmers from beginners to experts. Java is surely programming language of the future. If we look into the phases of program development, we come across many phases such as:





Step 1 : Creation of a Java program :-
By creating of a Java program we mean of writing of a Java program on any editor or IDE. This includes modifying of the program on editor, even after program has been written once. Creation of a Java program is not limit to only write program on editor and than leave it. Generally, creating of a Java program means writing a program on a editor or IDE, making all the corrections needed and than saving the program on secondary storage device as hard drive. After creating a Java program you should provide .java extension to file. It signifies that the particular file is a Java source code. Also , whatever progress we do from writing, editing , storing and providing .java extension to a file , it basically comes under creating of a Java program.

















Step 2 : Compiling a Java Program
Our next step after creation of program is the compilation of Java program. Generally Java program which we have created in step 1 with a .java extension, it is been compiled by the compiler. Suppose if we take example of our program say WelcomeJavaPrograms.java, when we want to compile this we use command such as javac. We can call it as The Java compiler. After opening command prompt or shell console we compile Java program with .java extension as
javac WelcomeJavaPrograms.java.

After executing the javac command, if no errors occur in our program we get a .class file which contains the bytecode. It means that the compiler has successfully converted Java source code to bytecode. Generally bytecode is been used by the JVM to run a particular application. Java Virtual Machine or JVM is a virtual machine that lies between our bytecode and underlying operating system. The final bytecode created is the executable file which only JVM understands.


















Java source code with .java extension is the high level programming language that only a programmer understands. While bytecode produced after compilation is the low-level language that only JVM understands. JVM is implemented on many operating systems which helps programmer to just compile program once, because the same .class file can be executed on different platforms with same JVM. This portion also provides portabilty feature, that you don't have to write again-and-again Java source code for the different platforms. As Java compiler is invoked by javac command, the JVM is been invoked by java command. On the console window you have to type :

java WelcomeJavaPrograms
This command will invoke the JVM to take further steps for execution of Java program.






















Step 3 : Program loading into memory by JVM:-
JVM requires memory to load the .class file before its execution. The process of placing a program in memory in order to run is called as Loading. There is a class loader present in the JVM whose main functionality is to load the .class file into the primary memory for the execution. All the .class files required by our program for the execution is been loaded by the class loader into the memory just before the execution.


































Step 4: Bytecode Verification by JVM :-
In order to maintain security of the program JVM has bytecode verifier. After the classes are loaded in to memory , bytecode verifier comes into picture and verifies bytecode of the loaded class in order to maintain security. It check whether bytecodes are valid. Thus it prevent our computer from malicious viruses and worms.












Step 5 : Execution of Java program : -
Whatever actions we have written in our Java program, JVM executes them by interpreting bytecode. If we talk about old JVM's they were slow, executed and interpreted one bytecode at a time. Modern JVM uses JIT compilation unit to which we even call just-in-time compilation. These compilers executes several instructions in parallel. They are also called as Java HotSpot compiler because JVM uses them to find hot spots in bytecode. By the term hot spots we mean that JVM analyze bytecode by searching those spots in program which are executed frequently. These frequent executable bytecodes parts are being translated to faster machine language code by Java HotSpot compiler. Now the significance behind this is whenever JVM encounter such parts again in bytecode it processes machine language code which are much faster as compared to execution of one byte code at a time.If we analyse above steps , we will find that there are two compilation phases. One in which source code is translated to bytecodes and second when hot spot parts of bytecodes are translated to machine language.

































Video Tutorial for above post :- 


 
© 2021 Learn Java by Examples Template by Hubberspot