-1

I am a novice to Java byte code and would like to understand the following byte code of Dispatch.class relative to Dispatch.java source code below :

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

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class B
       3: dup
       4: invokespecial #3                  // Method B."<init>":()V
       7: astore_1
       8: aload_1
       9: invokevirtual #4                  // Method A.run:()V
      12: return
}

//=====================Dispatch.java==============================
class Dispatch{   
   public static void main(String args[]){
      A var = new B();  
      var.run(); // prints : This is B
   }
}
//======================A.java===========================
public class A {
    public void run(){
         System.out.println("This is A");
    }
}
//======================B.java===========================    
public class B extends A {
    public void run(){
          System.out.println("This is B");
    }
}

After doing some reading on the internet I had a first grasp of how JVM stack and opcodes work. I still however do not get what these command lines are good for :

3: dup //what are we duplicating here exactly?
4: invokespecial #3 //what does the #3 in operand stand for?
invokevirtual VS invokespecial //what difference there is between these opcodes?
ecdhe
  • 421
  • 4
  • 17
  • What research have you done? Is there anything specific you want to be explained? – Antimony Apr 07 '18 at 20:42
  • @Antimony the thread has been updated – ecdhe Apr 07 '18 at 23:39
  • See [`invokespecial` instruction](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.invokespecial) and [Compiling for the Java Virtual Machine → Working with Class Instances](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-3.html#jvms-3.8), the [specification](https://docs.oracle.com/javase/specs/jvms/se8/html/index.html) has it all. Whatever you’ve read so far during your research, if it didn’t explain these things, it was not worth it. – Holger Apr 09 '18 at 08:50

1 Answers1

0

It really sounds like you need to read the docs some more, but to answer your updated questions,

  1. dup duplicates the top value on the operand stack. In this case, it would be the uninitialized B object that was pushed by the previous new instruction.

  2. The #3 means that invokespecial is operating on the 3rd slot in the classfile's constant pool. This is where the method to be invoked is specified. You can see the constant pool by passing -c -verbose to javap.

  3. invokevirtual is used for ordinary (non interface) virtual method calls. (Ignoring default interface methods for the moment) invokespecial is used for a variety of special cases - private method calls, constructor invocations, and superclass method calls.

Antimony
  • 37,781
  • 10
  • 100
  • 107