V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
JasonLaw
V2EX  ›  Java

关于“Inside the Java Virtual Machine - Chapter 5 The Java Virtual Machine - The Java Stack”的疑问

  •  
  •   JasonLaw · Sep 2, 2020 · 1452 views
    This topic created in 2075 days ago, the information mentioned may be changed or developed.

    在“Possible Implementations of the Java Stack”中,它给出了如下的 class:

    class Example3c {
    
        public static void addAndPrint() {
            double result = addTwoTypes(1, 88.88);
            System.out.println(result);
        }
    
        public static double addTwoTypes(int i, double d) {
            return i + d;
        }
    }
    

    然后它说:

    Note that the addAndPrint() method uses the constant pool to identify the addTwoTypes() method, even though it is part of the same class. Like references to fields and methods of other classes, references to the fields and methods of the same class are initially symbolic and must be resolved before they are used.

    The resolved constant pool entry points to information in the method area about the addTwoTypes() method. The virtual machine uses this information to determine the sizes required by addTwoTypes() for the local variables and operand stack. In the class file generated by Sun's javac compiler from the JDK 1.1, addTwoTypes() requires three words in the local variables and four words in the operand stack. (As mentioned earlier, the size of the frame data portion is implementation dependent.) The virtual machine allocates enough memory for the addTwoTypes() frame from a heap. It then pops the double and int parameters (88.88 and one) from addAndPrint()'s operand stack and places them into addTwoType()'s local variable slots one and zero.

    operand stack 只会存储一个 int 和一个 double,就像 local variables 一样。那么为什么 operand stack 需要 four words ? three words 不是够了吗?剩余的一个 word 是用来干嘛的?

    2 replies    2020-09-02 12:04:08 +08:00
    zsdroid
        1
    zsdroid  
       Sep 2, 2020
    “和”不需要保存吗?
    JasonLaw
        2
    JasonLaw  
    OP
       Sep 2, 2020
    我明白了,我 javac Example3c.java 之后,`javap -c Example3c.class `的输出结果如下所示:

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

    public static void addAndPrint();
    Code:
    0: iconst_1
    1: ldc2_w #2 // double 88.88d
    4: invokestatic #4 // Method addTwoTypes:(ID)D
    7: dstore_0
    8: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream;
    11: dload_0
    12: invokevirtual #6 // Method java/io/PrintStream.println:(D)V
    15: return

    public static double addTwoTypes(int, double);
    Code:
    0: iload_0
    1: i2d
    2: dload_1
    3: dadd
    4: dreturn
    }

    注意“1: i2d”,https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html 中的对 i2d 的描述是“Convert int to double”,这也就是为什么 operand stack 需要 four words 的原因。因为不是存储一个 int 和一个 double,是要存储两个 double 。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2961 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:59 · PVG 22:59 · LAX 07:59 · JFK 10:59
    ♥ Do have faith in what you're doing.