w3hJava

What, Why, When and How of Java, JavaFX and related technologies


Published June 9th, 2008

How Java handles overriding

One of the blog readers recently asked a question about “How Java handles overriding !”  So, I decided to make one entry 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 their hands 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

}

Published May 23rd, 2008

Java - Pass by Reference(Not possible)

I had a big time fight on my Orkut community with the concept that Java do things with Pass by Reference. Here is the one for Orkut users :

Here

But for non-orkut users let me recap the point again, its very important for a person who is new in java:

There is NO CONCEPT OF CALL BY REFERENCE IN JAVA, ONLY CALL BY VALUE IS POSSIBLE. We generally get confused in pass the object reference by value and passing by reference. Both are completely different.

Here is a small code, to get a more clear picture:

class MyClass {

String name;
int nameCode;

public MyClass(String name, int nameCode) {
this.name = name;
this.nameCode = nameCode;
}

public String toString() {
System.out.println(name + " : " + nameCode);
return (name + nameCode);
}
}

public class NoCallByReference {

public static void swap(MyClass a, MyClass b) {
MyClass temp = a;
a = b;
b = temp;
}

public static void main(String[] args) {
MyClass myclass = new MyClass(”Ramu”, 7);
MyClass yourclass = new MyClass(”Mohan”, 1);
swap(myclass, yourclass);
myclass.toString();
yourclass.toString();
}
}

A very simple code where I tried to swap two object of myClass. But you will surprise to see the output because after swapping even the value of myclass and yourclass will remain the same. Because the copy of myclass and yourclass has been created and get swapped rather than actual myclass and yourclass. It’s like

myclass — copyofmyclass
yourclass — copyofyourclass

Swapping is done on copyofmyclass and copyofyourclass. Better to go for a homework and run the command

javap -c NoCallByReference and try to figure our how assemble is going on :-).