Difference between byte code .<init>()V vs .<init>(Z)V

烈酒焚心 提交于 2019-12-13 11:41:46

问题


When I observe my Java project byte code, I see the following byte code :

java.lang.Object.()V

java.lang.Boolean.(Z)V

What is the meaning of <init>()V and <init>(Z)V


回答1:


java.lang.Object.()V

is a void method (V) on java.lang.Object that takes no arguments.

java.lang.Boolean.(Z)V

is a void method on java.lang.Boolean that takes a single boolean (Z since B is byte) argument.

In short,

 abc.def.WXYZ(IIIIIIIIIIIIII)J
 ^            ^              ^ 
 target_class argument-types return_type

See JNI Type Signatures for more detail.

The JNI uses the Java VM’s representation of type signatures. Table 3-2 shows these type signatures.

Table 3-2 Java VM Type Signatures

Type Signature             Java Type
Z                          boolean
B                          byte
...
L fully-qualified-class ;  fully-qualified-class
[ type                      type[]
( arg-types ) ret-type      method type

For example, the Java method:

long f (int n, String s, int[] arr); 

has the following type signature:

(ILjava/lang/String;[I)J



回答2:


It's all method signatures in bytecode used by JVM.

<init>()V and <init>(Z)V are construtor signatures. For JVM constructors are just as any other methods, they have a name, which is always <init>), and a return value, which is always V (means void). In our case Z means boolean parameter (B is reserved for byte)

that is

<init>(Z)V

in class Test's bytecode means

class Test {

    Test(boolean arg0) {
    }
}

you can also meet

 static <clinit>()V

which means static initialization block

static {
...
}


来源:https://stackoverflow.com/questions/14721852/difference-between-byte-code-initv-vs-initzv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!