Java如何将变量名与内存中的位置相关联? [英] How does Java associate a variable name with a location in memory?

查看:282
本文介绍了Java如何将变量名与内存中的位置相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java如何将变量名与内存中的位置相关联?

How does Java associate a variable name with a location in memory?

我首先想到的是,必须有一个用于所有变量名称的静态表,该表将其与其值或值的位置相关联.它存在吗?此表有正式名称吗?

My first thought about this is there must be a static table that is used for all variable names which associates it with either it's value or a location to it's value. Does it exist and is there a formal name for this table?

推荐答案

原始类型的变量的值是一个数字,而引用类型的变量的值是一个引用(通常是一个内存地址).

The value of variable of primitive type is some number and value of variable of reference type is a reference (usually an memory address).

现在的问题是:给定变量的存储值在哪里.这取决于变量的类型-有局部变量,实例变量(字段)和类变量(静态字段).

Now the question is: where is stored value of given variable. It depends on the the kind of variable - there are local variables, instance variables (fields) and class variables (static fields).

在编译过程中解析本地名称.每个变量都只是方法中的第i个变量,并将作为第i个变量存储在某些方法调用的堆栈帧中.

Names of locals are resolved during compilation. Each variable becomes simply i-th variable in method and it will be stored as i-th variable in stack frame of some method call.

对于实例变量,它将有所不同.字段名称始终以字节码形式出现(但通常不会出现在JIT编译器生成的机器代码中).给定类的所有对象都具有相同的布局,因此类可以存储给定字段的 offset -距对象开始的距离.解释器可以读取对象的地址并添加偏移量以计算变量的存储位置.

For instance variables it will be different. Field names are always present in bytecode (but will generally not be present in machine code generated by JIT compiler). All objects of given class have the same layout, so class can store offset of given field - distance from beginning of the object. Interpreter can read the address of object and add offset to calculate where is variable stored.

类变量类似于实例变量,但更简单.在这种情况下,类会存储其变量的名称和值.

Class variables are similar to instance variables, but simpler. In this case, class stores both names and values of its variables.

这篇关于Java如何将变量名与内存中的位置相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆