Java bytecode: type of local variable?

According to this article http://slurp.doc.ic.ac.uk/pubs/observing/linking.html#assignment :

My question: why does bytecode not contain the type information of local variables, but does contain the type information of parameters and return values?

Solution

First, there are several different types of concepts There are compile - time types, including generics However, the imitation code does not exist after compilation

There is a static type inferred by the verification of a variable, which can be int, float, long, double, returnaddress or object reference Object references are typed with an upper limit, so all references are subtypes of Java / Lang / string Fields can have another short type: byte, short, char, or Boolean These are considered the same as the execution purpose, but have different storage space

Finally, there is the same runtime type as the validated static type, but in the case of object reference, represents the actual type of the referenced instance Note that due to the lazy validator, in some cases, the runtime type may not actually be a subtype of the validation type For example, a variable of type comparable declared can actually save any object in hotspot because VM does not check the interface during validation

Compile time information will not be preserved except through optional attributes of reflection and debugging This is because there is no reason to keep it

Local variables have no explicit type information (except for the new stackmaptable attribute, but this is a technical one) Conversely, when the class is loaded, the bytecode validator infers the type of each value by running static data flow analysis The purpose of this is not to catch errors like compile time type checking, because it is assumed that bytecode has been checked at compile time

Instead, the purpose of verification is to ensure that the instructions are not dangerous to the VM itself For example, it needs to ensure that you do not take integers and interval them as object references, because this can lead to arbitrary memory access and hacking

So although bytecode values have no explicit type information, they have an implicit type, which is the result of static type inference These details vary according to the internal implementation details of each virtual machine, although they should follow the JVM standard But you just need to worry about handwritten bytecode

Fields have explicit types because the VM needs to know which type of data is stored in them Method parameters and return types are encoded in so - called method descriptors and are also used for type checking They cannot be inferred automatically because these values can come from or go anywhere, and type checking is performed on a per class basis

PS: I omitted some details when talking about validation types Object types also track whether they are initialized and which instructions create them when not initialized Address types track the target of the JSR that created them

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
分享
二维码
< <上一篇
下一篇>>