Sunday, December 09, 2007

How Java handles Method ?

In my previous blog "Performance - Final Keyword ? " One of the blog readers asked question about How Java handles overriding ! So, I decided to make one entry(I guess more than one) for the answer.

Yes overriding tactics in Java is very different from C++ as methods by default in Java can be overridden unlike C++. In C++, the concept of overriding functions are handled by Virtual Table, VTable(This wiki link contains lot of information). Whereas in Java there is some other concept. Before going into the depth, let's see some of the basic things which one should need to know before making hand dirty in overriding concept.

Here is a Simple HelloWorld Program:

class Hello {
public static void main(String[] args)
{
System.out.println("Hello Bloggers!");
}
}

Lets see what the bytecode is generating, javap -c Hello(more about javap)

Compiled from "Hello.java"
class Hello extends java.lang.Object{
Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2; //class Hello
3: dup
4: invokespecial #3; //Method "":()V
7: astore_1
8: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
11: ldc #5; //String Hello it is
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
16: return

}

Have a look on these lines:

1: invokespecial #1; //Method java/lang/Object."":()V
4: invokespecial #3; //Method "":()V
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V

These are the lines related to method invocation. So what the heck is this invokespecial and invokevirtual ? Actually JVM used 4 different kinds of instructions for method invocation those are :

- invokevirtual - This is for instance method like System.out.println("Hello Bloggers!") here.
- invokestatic - This is for class methods.
- invokespecial - This is for special things. It is used when
- call , instance initialization.
- super call, when you will call something from super.method
- private methods. As private methods can't be overridden so we need to put this in a special category.
- invokeinterface - invoking instance method with interface reference(Soon we will see the example)

Now we are very clear that why invokespecial has been used at #1 and #3 whereas invokevirtual at #6. Ok, lets write some code which can see the usages of all four.


interface interfaceForHello {
public void noUse();
}

class Hello implements interfaceForHello {
public void noUse() {
System.out.println("No use");
}
public static void staticMethod()
{
System.out.println("Static method");
}
public static void main(String[] args)
{
interfaceForHello iface = new Hello();
iface.noUse();
Hello.staticMethod();
System.out.println("Hello Bloggers ! ");
}
}

And here goes the javap -a Hello:

Compiled from "Hello.java"
class Hello extends java.lang.Object implements interfaceForHello{
Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public void noUse();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String No use
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return

public static void staticMethod();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #5; //String Static method
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return

public static void main(java.lang.String[]);
Code:
0: new #6; //class Hello
3: dup
4: invokespecial #7; //Method "":()V
7: astore_1
8: aload_1
9: invokeinterface #8, 1; //InterfaceMethod interfaceForHello.noUse:()V
14: invokestatic #9; //Method staticMethod:()V
17: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
20: ldc #10; //String Hello Bloggers !
22: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
25: return

}

In next blog we will continue the same.

5 comments:

Anonymous said...

Good Blog!

Please continue on it.

Anonymous said...

It is remarkable, very valuable phrase

Anonymous said...

Good fill someone in on and this fill someone in on helped me alot in my college assignement. Say thank you you seeking your information.

Anonymous said...

Brim over I to but I contemplate the brief should secure more info then it has.

Anonymous said...

i easily love all your posting way, very useful,
don't give up as well as keep writing seeing that it just truly worth to look through it,
looking forward to look into much of your content articles, have a pleasant day!