Monday, 5 December 2016

Complete flow of JVM compilation and run program.

Complete flow of JVM compilation and run program.

Complete flow of JVM.


1.   JVM loaded in the memory.
2.   Class loader invoked by the JVM.
3.   Test.class file loaded by the class loader.
4.   .class file provided to the byte code verifier.
5.   After the successful verification class loaded into the class area.
6.   main() method started.
7.   Frame of the main() method created.
8.   Execution of the instruction of the main() method.
      9. Execution of main() method completed and frame gets destroyed.

Steps to make the .jar file

Steps to make the .jar file

Step 1: - prepare the .class file by the javac these are the .class file that we want to move in the .jar file.
               C:\java>javac abc.java
.class will be generated.

Step 2: -Execute the jar command
command to make jar file
Step 3: - After creating the sample.jar file just place it in the jre\lib\ext folder.
Step 4: - Delete the .class file from the current directory
Step 5: - Run the program java Test
                                                That contains main method
Step 6: - System class loader becomes fail to load Test.class but extended class loader will load successfully from 
            C:\Program Files\java\jdk1.8.0\jre\bin\ext\sample.jar

THE ARCHITECTURE OF THE JVM

THE ARCHITECTURE OF THE JVM.

Architecture of JVM


1.      Class loader.
2.      Byte code verifier.
3.      Memory area.
i.              Class area.
ii.            Heap area.
iii.          Stack area.
iv.          Native area.
v.            PC register.
    4. Execution engine.

1.      Class loader
               Class loader is the utility in the JVM which is responsible to load the .class file from hard disk to JVM.

There are Three Types of class Loader
1.1      Boot Strap class loader
1.2      System class loader
1.3      Extended class loader

1.1     Boot Strap class loader
·         Boot strap class loader loads the .class file from the java library available in the JRE.
   C:\Program Files\java\jdk1.8.0\jre\lib\rt.jar
a.       .jar file is the compressed file.
b.      rt.jar file contains the approximately 5000 classes within the different packages.

[Note: - If were using the class of the java library in our class then all that classes also gets loaded in the JVM.]

For Ex: - In the last program System class, Sting class and Integer class are the library classes that we were using in our Test class.
·         Along with our test class there are three library classes also gates loaded into the JVM.

1.2     System class loader
·         System class loader is responsible to load the user define classes from the class path.
·         The current directory that we are opened in the DOS is the default class path.

1.3     Extended class loader
               This class loader will load the .class file from the jre\lib\ext folder.
         C:\Program Files\Java\jdk1.8.0\jre\lib\ext

               In the ext folder the .class file should not be placed directly rather must be placed within the .jar file.
         Jar tool in java is use to make a .jar file and also use to uncompress the .jar file.
               .jar tool is also available in the
         C:\Program Files\Java\jdk1.8.0\jre\bin


Steps to make the .jar file

Step 1: - prepare the .class file by the javac these are the .class file that we want to move in the .jar file.
               C:\java>javac abc.java
.class will be generated.

Step 2: -Execute the jar command
command to make jar file
Step 3: - After creating the sample.jar file just place it in the jre\lib\ext folder.
Step 4: - Delete the .class file from the current directory
Step 5: - Run the program java Test
                                                That contains main method
Step 6: - System class loader becomes fail to load Test.class but extended class loader will load successfully from
            C:\Program Files\java\jdk1.8.0\jre\bin\ext\sample.jar


Sequence of the class loader to load any class
1.      Extended class loader.
2.      System class loader.
3.      Boot strap class loader 



2.     Byte code verifier
·         Byte code verifier is the tool in the JVM that verify to the code of the .class file loader by the class loader
·         In the content in the .class file is found correct by the byte code verifier than the content of the .class file copied into the memory area of the JVM. 

3.     Memory area
               Memory area in JVM there are five types of memory areas in the JVM
3.1      Class area
3.2      Heap area
3.3      Stack area
3.4      Native area
3.5.    PC register

3.1   Class area
·         Class areas in the JVM are used to hold the .class file which is loaded by the class loader.
·         They are only one class area in the JVM which is shared by all the threads.
 
Class area

3.2     Heap Area
               All the objects always are created in the heap area.
               There is only one heap area as per JVM which is shared by all the thread running in the                        JVM.
            
Heap area
3.3     Stack Area
               Stack area is used in the method execution.
               That means when any method is invoked then immediately a new frame is created in the                      stack and as the method completed then the frame of that method gets destroyed.
Stack area

 [Note: - There is the separate stack in the JVM for the separate threads.]

3.4     Native Area
·         Native area in the JVM is use to contain the native code.
·         A native code is the code written in java in the special manner by using the JNI (Java native Interface).
·         Through the native code is the function of the C & C++ can be invoked.
·         In the operating system the internal library is developed in the C language that is invoked by JVM through the native code.
·         There is a separate native code for the separate thread.

3.5     PC Register
·         A PC register contains the next instruction to be executed.
·         There are shape rate PC register for each thread.

4. Execution engine.
The bytecode that is assigned to the runtime data areas in the JVM via class loader is executed by the execution engine. The execution engine reads the Java Bytecode in the unit of instruction. It is like a CPU executing the machine command one by one.

Working of JIT (Just In Time) compiler.

 Working of JIT (Just In Time) compiler.

  • JIT instance for just in time compiler which is responsible to convert the byte code into the executable code in the following manner.
  • When a method calling is encountered first time, the JIT converts there instructions and provided then to the operating system for the execution and at that same time also stored the converted instruction in the temporary cache.
  • When the method calling this encountered again then the JIT fix up the converted instructions from the temporary cache and provided to the operating system.
JIT working

1.      Java command issued to run the Test class and immediately JVM gets loaded into the RAM.
2.      Test class loaded into JVM that means byte code of the total class loaded.
3.      JIT started to convert the instruction of main method.
4.1      M1 ( ) method calling encountered.
4.2  Instruction of M1 ( ) method converted by the JIT and stored into the cache.
4.3  Instruction provide to the operating system for the execution.
5.1  Calling of M1 ( ) method again.
      5.2 JIT pickup the instruction from the cache and provide to the operating system.

Difference between Compiler and Interpreter

Difference between Compiler and Interpreter



  • Initially JVM in java was the interpreter but now the JVM is the compiler come interpreter both.
  • As the conversion tool JVM has JIT (Just In Time Compiler).

Except the value from command line and find out the minimum and maximum values.

Except the value from command line and find out the minimum and maximum values.



Except the value from command line and check how many values are even or odd?

Except the value from command line and check how many values are even or odd




Creating a java program to calculate the sum of value enter from the command line.

Creating a java program to calculate the sum of value enter from the command line.



A java program to get the command line value using loop.

A java program to get the command line value using loop.


Command Line Argument

Command Line Argument

  • Command line argument is the way by which we can provide the values to the program from outside the program.
  • The java tool provide the syntax to pass the command line argument.
Give the argument at run time

Ex:
Command line argument

  • All the command line argument is automatically stored into the array and that array is automatically passed into the argument of main method by the JVM.
Example: -A java program to get the command line values.





All the command line argument separated by the space.

[Note: -in the java each array has the length properties that specify the no of elements content by the array.]

Sunday, 4 December 2016

To print primed 5.

To print primed 5.

To print primed 4.

To print primed 4.


To print primed 3.

To print primed 3.


To print primed 2.

To print primed 2.

To print primed 1.

To print this Series 0 1 1 2 3 5 8 13.......n.

To print this Series 0 1 1 2 3 5 8 13.......n.

To find a power b no.

To find a power b no.

To find the no is prime

To find the no is prime


To find the Factorial no.

To find the Factorial no. 

Syntax of valid .java file.

Syntax of valid .java file

package p1; --------->>     package Declaration
import my package.*; --------->>     package importing
@TestAnnotation --------->>     use the annotation

class <Class_name>
{
Variable declaration;
Method declaration;
}


Components of the our 1st program Demo.java

class Demo
class    ---------->>          keyword
Test     ---------->>          identifier (user defined name)


public static void main(String []args)
public    --------->>        Access specifier                        ----------->>           keyword
static      --------->>        Access modifier                       ----------->>           keyword
void       --------->>        Return type of main method     ----------->>           keyword
main       --------->>        Method name
String    ---------->>       Library class of java
[]args     ---------->>       Identifier(name of Array)


System.out.println();
System     --------->>        Library class
out           --------->>        Object of PrintStream class
println      --------->>        Method of PrintStream

Println: - after println the statement println place the cursor on the new line.
System.out.print();

Print: - It wills the cursor in same line after printing the same line.



First java program

First java programTest.java

Output


[ Note: - Byte code- it contains 1byte, basically its shorthand style code ]

All the programming in java always is performed within the class. Except the declaration, package import and the class level annotation.

Setting the Class Path in JAVA

Setting the  Class Path in JAVA
In order to use the java tools in the MSDOS and in order to use the java tools by the other software such as web server, IDE’s etc.


They are two ways to set the path
  1. Temporary path setting:-
    Set path = C:\Program Files\Java\jdk 1.8.0\bin

    [Note: - Temporary path work only for the current session of the DOS ]


    To view the entire path those are already set in MSDOS prompt
    Set path 

  2. Permanent path setting
  3.    Copy the java\bin path.
             Right click on my computer --------->>Select properties
             Advance System Setting --------->>
             Select Environment variables.
             Select path variable ------->> Edit ------>> paste path ---->> OK.

Development Process and tool in JDK.

Development Process

  • Development always perform three task in their development process
    • Writing the source code.
    • Compilation of the source code.
    • Testing (execution) of the compiled code.
[Note: - in the JDK doesn’t provide any IDE (integrated development environment) we have to install them separately. IDE’s of java Ex: - netbeans, Eclipse, Myeclipse. ]

Now we can write the source code in the notepad or any other text editor and we can use the development tools in the MSDOS.

Development Tool in the JDK
  1. JAVA TOOLS: - This tool is the java compiler that is used to compile the source code and generate the byte code.
  2. JAVA TOOL: - This is use to start the JVM to run the byte code.
    • JVM started
    • .class file loaded into the JVM
    • Executable code prepared
    • Executable code to provide to the operating system by the JVM for the execution.
  3. JAVAP: - This tool is use to list out the details of any class.
  4. JAVADOC: - This tool is use to create the technical documentation of the java classes.
  5. JARTOOL: - This tool is use to make the .jar file.