Saturday, March 2, 2013

Internal work of JVM




In our previuos section we have learned first java program 'helloworld", how to compile and hoe to run,now we are going to learn how the program is compliling and running and also what is the JVM ,and its internal flow,moreover some question on this first java program.
what happens at Compile time?
After compiled the "helloworld.java" by java compiler using javac.it will convert the java code into byte code.
% javac HelloWorld.java

After the execution of this command,the .class file i.e:HelloWorld.class is created.
In other words,it is the dos commad given to the java compiler to convert contents of the mentioned source into its equilant byte code.

What happens at  Runtime?
Now here JVM role comes,here classloader will loads the "System.lang.classloader"  finds the Helloworld class by searching the class path given.
Interpreter will read the byte code stream and then transfer to JVM.
JVM will extracts the all the information from the bytecode stream,as follows
Constants like literals,symbolic refernces to types,fields,methods will be stored in constants pool.
methods information like modifers,type ,static variables,package,return type,method parameters will stored in "Method area".
Once extracts the information ,JVM will find for public static void main(String args[]).
JVM memory consists following segments.
Heap Memory, which is the storage for Java objects
Non-Heap Memory, which is used by Java to store loaded classes and other meta-data
JVM code itself, JVM internal structures, loaded profiler agent code and data, etc.
In this program public,static,void are keywords.
main() is a method.
String is a class.
println is a method and system is a class.

constants( like literals, constants, symbolic references to types, fields, and methods) will be stored in constant pool [in this case, symbolic reference to HelloWorld class and the fields, methods, constants associated with it]
package,modifiers,static variables [in this case "HELLOWORLD" attribute],
field information(name,type,modifiers) -
methods information(name,return type,method parameters,modifiers, method’s bytecode) – in this case , its [print, void, public and byte code]
reference to ClassLoader [ which classloader loads this class ]
reference to class Class
and store the above information in “Method Area“.
After loading that information, i as jvm(thread) will try to find out “public static void main(String [] args)” method.
Now the following command is given to the JVM to execute the .class file.
% java HelloWorld

In the process of executing the .class file one of the responsibility of the JVM is to convert the .class file into equilant executable code.

Java First Program



Java Program :
We break the process of programming in Java into three steps:
  1. Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java.
  2. Compile it by typing "javac MyProgram.java" in the terminal window.
  3. Run (or execute) it by typing "java MyProgram" in the terminal window.
The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file namedMyProgram.class); the third actually runs the program.
  • Creating a Java program. A program is nothing more than a sequence of characters, like a sentence, a paragraph, or a poem. To create one, we need only define that sequence characters using a text editor in the same way as we do for e-mail.HelloWorld.java is an example program. Type these character into your text editor and save it into a file named HelloWorld.java.
public class HelloWorld { 
   public static void main(String[] args) { 
      System.out.println("Hello, World");
   }
}
  • Compiling a Java program. At first, it might seem to you as though the Java programming language is designed to be best understood by the computer. Actually, to the contrary, the language is designed to be best understood by the programmer (that's you). A compiler is an application that translates programs from the Java language to a language more suitable for executing on the computer. It takes a text file with the .java extension as input (your program) and produces a file with a .class extension (the computer-language version). To compile HelloWorld.java type the boldfaced text below at the terminal. (We use the % symbol to denote the command prompt, but it may appear different depending on your system.)
% javac HelloWorld.java
If you typed in the program correctly, you should see no error messages. Otherwise, go back and make sure you typed in the program exactly as it appears above.
  • Executing a Java program. Once you compile your program, you can run it. This is the exciting part, where the computer follows your instructions. To run the HelloWorld program, type the following at the terminal:
% java HelloWorld
If all goes well, you should see the following response
Hello, World
  • Understanding a Java program. The key line with System.out.println() send the text "Hello, World". When we begin to write more complicated programs, we will discuss the meaning of public, class, main, String[], args, System.out, and so on.
  • Creating your own Java program. For the time being, all of our programs will be just like HelloWorld.java, except with a different sequence of statements in main(). The easiest way to write such a program is to:
    • Copy HelloWorld.java into a new file whose name is the program name followed by .java.
    • Replace HelloWorld with the program name everywhere.
    • Replace the print statement by a sequence of statements.

Errors.

 Most errors are easily fixed by carefully examining the program as we create it, in just the same way as we fix spelling and grammatical errors when we type an e-mail message.
  • Compile-time errors. These errors are caught by the system when we compile the program, because they prevent the compiler from doing the translation (so it issues an error message that tries to explain why).
  • Run-time errors. These errors are caught by the system when we execute the program, because the program tries to peform an invalid operation (e.g., division by zero).
  • Logical errors. These errors are (hopefully) caught by the programmer when we execute the program and it produces the wrong answer. Bugs are the bane of a programmer's existence. They can be subtle and very hard to find.
One of the very first skills that you will learn is to identify errors; one of the next will be to be sufficiently careful when coding to avoid many of them.

Input and output.

 Typically, we want to provide input to our programs: data that they can process to produce a result. The simplest way to provide input data is illustrated inUseArgument.java.Whenever this program is executed, it reads the command-line argument that you type after the program name and prints it back out to the terminal as part of the message.
% javac UseArgument.java
% java UseArgument Alice
Hi, Alice. How are you?
% java UseArgument Bob
Hi, Bob. How are you?

Internal works of Helloworld programm

In our previuos section we have learned first java program 'helloworld", how to compile and hoe to run,now we are going to learn how the program is compliling and running and also what is the JVM ,and its internal flow,moreover some question on this first java program.
what happens at Compile time?
After compiled the "helloworld.java" by java compiler using javac.it will convert the java code into byte code.
% javac HelloWorld.java
After the execution of this command,the .class file i.e:HelloWorld.class is created.
In other words,it is the dos commad given to the java compiler to convert contents of the mentioned source into its equilant byte code.