Java Virtual Machine command dup understanding, Java Virtual Machine dup

Source: Internet
Author: User

Java Virtual Machine command dup understanding, Java Virtual Machine dup

For example:

1 public class ExceptionTest{2 3     void cantBeZero(int i) throws Exception{4         throw new Exception();5 6     }7 8 }

The bytecode command after the above Code is compiled is as follows:

 1  void cantBeZero(int) throws java.lang.Exception; 2    descriptor: (I)V 3    flags: 4    Code: 5      stack=2, locals=2, args_size=2 6         0: iload_1 7         1: ifne          12 8         4: new           #2                  // class java/lang/Exception 9         7: dup10         8: invokespecial #3                  // Method java/lang/Exception."<init>":()V11        11: athrow12        12: return

1) The new command allocates memory space for the Exception object on the java heap and pushes the address to the top of the operand stack;

2) then the dup command is the top value of the copy operand stack and pushes it to the top of the stack. That is to say, at this time, the operand Stack has two consecutive identical object addresses;

3) The invokespecial command calls the instance initialization method <init> :() V. Note that this method is an instance method, so a this reference needs to be popped up from the top of the operand stack, that is to say, this step will pop up an object address in the previous stack;

4) The athrow command extracts a reference type value from the top of the operand stack and throws it;

5) The method ends with the return command.

From the five steps above, we can see that two instance objects need to be referenced from the top of the stack, which is why there is a dup command under the new command, in fact, for every new instruction, the compiler generally generates

A dup command, because the initialization method of the instance must be used once, and the second is left for programmers to use, such as assigning values to variables and throwing exceptions, the compiler will also generate dup commands and pop them out from the top of the stack after the initialization method is called. For example, we create only one object without any operation, for example:

1     void cantBeZero(int i) throws Exception{2          new Exception();3 4     }

The above Code only creates an Exception object without any operation.

The compiled bytecode command is as follows:

 1  void cantBeZero(int) throws java.lang.Exception; 2    descriptor: (I)V 3    flags: 4    Code: 5      stack=2, locals=2, args_size=2 6         0: new           #2                  // class java/lang/Exception 7         3: dup 8         4: invokespecial #3                  // Method java/lang/Exception."<init>":()V 9         7: pop10         8: return

A dup command will also be generated, but after the instance initialization method is called, duplicate instance references will pop out of the stack. However, this situation basically does not appear in our code, because every object we create should be useful.

Through the above example, you should have a clear understanding of why there is always a dup command when creating an object.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.