HelloWorld In JVM’s View – How Java Program executed internally in JVM

August 23, 2012 at 6:49 pm 14 comments

Every java developer come across this “Hello World” that kick start us in objectville trip. But while startup, we are more concentrating in understanding language rather than thinking how the internal works. It may be true in most case. So i thought to show Hello world in Jvm’s view.

package com.wordpress.kkarthikeyanblog;

public class HelloWorld {

public static String HELLOWORLD = "Hello World";

public void print() {
System.out.println(HELLOWORLD);
}

public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.print();
}
}

After compiled the above code using javac. I am running the HelloWorld using the following command. Now JVM triggered.

java com/wordpress/kkarthikeyanblog/HelloWorld

JVM Explanation :

hey Guys, its me JVM .. If you dont know about me, just get a heads up in link 

I am going to explain, how i(jvm) handle the class file and execute it.

As i start, BoostrapperClassLoader load the essential java.lang package and “System Class Loader” inside me find the class “Hello world” by searching the classpath given. After locating the binary class file(HelloWorld.class) , it will transfer this binary stream to Me(JVM). I(JVM) extracts the following information from the binary stream of HelloWorld.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.

As you know, every thread in me(JVM), apart from shared “Method area” and “Heap Space“, there will be individual “stack” and “pc register”. That will help every thread of me in execution further.

i will push that main() method in stack as one stack frame by getting those info from Method Area’s method bytecode. Program Counter(pc register) will tell me(jvm thread) which line to execute next.

so as PC instructing me, i(jvm thread) start with the line

HelloWorld helloWorld = new HelloWorld();

I will get the symbolic reference(fully qualified name) of HelloWorld from constant pool and i do the lookup in method area.From Method area ,i get the class info and create the object with default initial value in “Heap space”. After creating the object, i will feed the object reference to stack method local variable.

Now PC will point to

helloWorld.print();

i(jvm) will take the local variable “helloWorld” reference in my stack and find the method print(). After getting byte code info from Method Area, i will push method “print()” in the stack as another stack frame, now i will start executing print() method.

Once the print() method is executed as before, that method will be pop up and it continue executing main() method. Once main() method ends , that method also pops up from the stack and I am done with my execution.

In Summarizing the above, In JVM, we have

Method area – to store the class metadata [as i mentioned before]
Heap Space – to hold only the objects

Specific to each Thread :
Stack – consists of stack frames[i.e Methods] – it will also hold the local variables specific to the method
Program Counter Registers – To guide what to be executed next

Apart from these, we have “Garage Collector” to free unreferenced objects.

I hope you will get a overall idea of how “hello world” program executed internally in jvm.

About these ads

Entry filed under: JVM. Tags: , , , , , .

HTTPS (SSL) Configuration : in Tomcat / Jetty / Jboss Connection Pooling : What, Why, How ?

14 Comments Add your own

  • 1. sathiya  |  August 24, 2012 at 5:53 pm

    Hey its really gud.Gathering info and making it crisp is really a gud job..Please carry on these activities.Gud work..

    Reply
  • 2. Benson  |  August 27, 2012 at 12:57 pm

    It is very nice of you to describe jvm’s work internally.Looking forward to your next blog…

    Reply
  • 3. armurerie  |  October 22, 2012 at 12:45 pm

    Outstanding story there. What occurred after? Good luck!

    Reply
  • 4. geld op het internet verdienen  |  October 23, 2012 at 5:58 pm

    Heya i’m for the primary time here. I found this board and I find It really useful & it helped me out a lot. I’m
    hoping to give one thing back and help others such as
    you aided me.

    Reply
  • 5. Test.com  |  December 13, 2012 at 12:16 pm

    Hey there! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really appreciate your content. Please let me know. Many thanks

    Reply
    • 6. nayekihtrak  |  December 14, 2012 at 6:13 am

      Please goahead… blog is to share :)

      Reply
  • 7. Srinivas  |  May 16, 2013 at 11:07 am

    Awesome

    Reply
  • 8. pankaj kumar jha  |  June 21, 2013 at 4:05 am

    Really this great explanation.Very thanks to you.

    Could you please post about how memory manage my JVM internally and controlling memory leak .How a profiler can help to control memory leak.

    Reply
    • 9. anusha  |  July 7, 2013 at 2:27 pm

      thanku it realy helped me in understandn d concept :)

      Reply
  • 10. jagadeesh  |  July 28, 2013 at 5:08 am

    it s not good improove it

    Reply
    • 11. jagadeesh  |  July 28, 2013 at 5:09 am

      its very usefull and meaning full

      Reply
  • 12. Prasad  |  November 1, 2013 at 10:49 am

    really good

    Reply
  • 13. Sheeraz  |  September 22, 2014 at 3:23 pm

    Great explanation .

    Reply
  • 14. venkey  |  October 10, 2014 at 6:10 am

    It really helped me.could you please how to mange hexa decimal format in jvm

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Blog Stats

  • 19,072 hits

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: