2

Let's consider the following piece of code:

package p;
public class Simple {

public static void main(String[] args){
    System.out.println("HELLO!\n");
   }
}

and JVM code for that ( javap -c Simple.class):

Compiled from "Simple.java"

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

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String HELLO!\n
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}

And I have a questions:

  1. Probably, the entry point is the main function. But, before that, it is neccesary to invoke a constructor for Simple class. So, how does it work? I mean, who calls firstly the construactor and then pass a control to main? If I am wrong please explain me how the control flow works.

  2. Instructions like getstatic take arguments like #2 . What does it mean #2?

  3. Comment looks like: // Method java/lang/Object."<init>":()V What does it mean V?

  4. How does the control flow work?

Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
  • "But, before that, it is neccesary to invoke a constructor for Simple class." No, it is not. `main` is a __static__ method. – tkausl Oct 12 '16 at 10:31
  • I see no constructor being *invoked*. Yes one is created by default for any class without an explicit constructor, but I don't see one being invoked anywhere in the code above. – Hovercraft Full Of Eels Oct 12 '16 at 10:33
  • Ok, so please explain me how the control flow works. – Gilgamesz Oct 12 '16 at 10:33
  • 2
    See [how does jvm enter in public static void main?](http://stackoverflow.com/questions/24557989/how-does-jvm-enter-in-public-static-void-main/24558676#24558676) – apangin Oct 12 '16 at 12:15

2 Answers2

2
  1. The Java compiler automatically generates a constructor when none is defined explicitly. In your example, it's not called at all. It's just that autogeneration policy.
  2. It is a reference to the PrintStream property out on System. The Java compiler automatically converts import statements into fully qualified object declarations.
  3. V means void
  4. Probably something like this:
    1. Load the reference.
    2. Create an anonymous String object with that value.
    3. Push a stack frame into place that jumps to the println method's entry point with reference to #2 as the parameter.

What #2 means:

import java.util.List;

List x;

becomes simply this:

java.util.List x = new java.util.ArrayList();

when the compiler generates bytecode. java.lang is automatically imported.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • So, what is `#X`? Does the compiler "scans" for constants/object declarations and put it in array indexed by #X? "The Java compiler automatically generates a constructor when none is defined explicitly. In your example, it's not called at all. It's just that autogeneration policy." Ok. If I defined my own constructor for Simple class, when it will be called? – Gilgamesz Oct 12 '16 at 11:26
  • 2
    @Gilgamesz `#X` is the index into the constant pool. See ["The class File Format"](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html) – apangin Oct 12 '16 at 11:54
  • The example is misleading. The sole declaration `List x;` does not suddenly include the instantiation of `java.util.ArrayList` after compiling. – Holger Oct 13 '16 at 11:22
0
  1. Constructor is not called in this case because main() is static.

  2. getstatic gets the value of System.out static field

  3. V means void, that is main() return type is void

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • "getstatic gets the value of System.out static field" Ok, But, where is it? Is the value a reference? "Constructor is not called in this case because main() is static." Ok, that is true. main() is static so it cannot call constructor. Does it mean that constructor is not called at whole? – Gilgamesz Oct 12 '16 at 10:49