Java – who will automatically pack / unpack?
•
Java
Does the compiler or runtime perform automatic boxing / unpacking?
Consider the following example:
public Integer get() { return 1; //(1) }
At (1), the original integer value will be converted to something like new integer (1) and returned This is actually an invisible member called automatic boxing, but who would do so? Compiler or JVM?
I've just started learning ASM. I'm very confused about such boxing problems
Solution
You can view the disassembly code using the javap - C command:
public class Example { public Example(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public java.lang.Integer get(); Code: 0: iconst_1 1: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: areturn }
You can see that integer #valueof is called, so the actual code is converted to:
public Integer get(){ return Integer.valueOf(1); }
Conclusion:
The compiler does it for you
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码