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?

4
  • "But, before that, it is neccesary to invoke a constructor for Simple class." No, it is not. main is a static method. Commented Oct 12, 2016 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. Commented Oct 12, 2016 at 10:33
  • Ok, so please explain me how the control flow works. Commented Oct 12, 2016 at 10:33
  • 2
    See how does jvm enter in public static void main? Commented Oct 12, 2016 at 12:15

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 #X is the index into the constant pool. See "The class File Format"
The example is misleading. The sole declaration List x; does not suddenly include the instantiation of java.util.ArrayList after compiling.
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

1 Comment

"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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.