英文:
Where are method reference copies stored in java?
问题
当您调用带有传递字段的方法时,会在堆栈中创建该字段值的副本。这意味着如果它是原始数据类型,通过更改此变量的值,您不会更改传递字段的值。但是,如果您更改堆中的值,引用将是副本,但仍引用相同的内存,这意味着您可以更改传递字段的值。
我的问题是,这些临时变量存储在哪里?如果它们是引用,它们应该存储在堆栈中,对吗?但是堆栈具有静态内存分配,如果在代码运行时调用方法,它们必须存储在动态分配的内存中,对吗?所以这是一个矛盾之处。
抱歉,如果这些问题听起来愚蠢,我是一个初学者程序员,正在努力正确理解程序如何与内存一起工作。
英文:
When you invoke a method with a passed field, a copy of this field's value in stack is created. This means if it is a primitive type, by changing the value of this variable, you will not change the value of the passed field. However if you change a value in heap, the reference will be a copy, but still reference the same memory, meaning you can the value of the passed field.
My question is where are these temporary variables stored? if they are references they should be stored in stack correct? but stack has static memory allocation and if methods are invoked when the code is running, they must be stored in dynamically allocated memory correct? So it is a contradiction.
Sorry if this questions sounds dumb, I am a novice programmer, and am trying to understand properly how programs work with memory.
答案1
得分: 1
> ... 但是栈具有静态内存分配,如果在代码运行时调用方法,那么这些方法必须存储在动态分配的内存中,对吗?
不正确。栈上的分配不是静态的,而是动态的。当调用方法时,会添加堆栈帧,并在方法调用退出时将其移除。
所有局部变量都保存在线程堆栈上的相应方法调用的堆栈帧中。这对于变量是原始类型还是引用类型都是正确的。
我不完全确定你所说的 "method reference copies" 是什么意思。
从Java 8及更高版本开始,支持一种称为 "方法引用" 的东西,因此我不确定你是在谈论这些内容还是在谈论具有引用类型的方法局部变量。
如果你在谈论 "方法引用",那么答案是它们是引用类型,并且像其他引用类型一样表示。有一个引用(指针),它指向堆中保存 "方法引用" 状态的节点。这个引用(指针)可以保存在堆栈上的局部变量中,在静态变量中,或者在其他驻留在堆中的对象的字段/元素中。
最后:
> 我是一个新手程序员,正在努力理解程序与内存的工作原理。
我的建议是不用太担心这些。要成为一个有效和熟练的Java程序员,并不需要理解Java程序如何与内存一起工作。Java运行时系统会处理所有这些...所以你不需要理解它。
我的建议是:
- 等到你成为经验丰富的Java程序员。
- 等到你完成了关于计算机硬件体系结构和编译器编写的学位课程。
- 等到你需要理解这些内容的时候再考虑。
英文:
> ... but stack has static memory allocation and if methods are invoked when the code is running, they must be stored in dynamically allocated memory correct?
Incorrect. Allocation on the stack is not static. It is dynamic. Stack frames are added when a method is called, and removed when the method call exits.
All local variables are held in the stack frame of the respective method call on a thread's stack. This us true whether the variable has a primitive type or a reference type.
I'm not entirely sure what you mean by "method reference copies".
Java 8 and later have support for things called "method references", so I'm not sure if you are talking about those or about a method's local variables that have a reference type.
If you are talking about "method references", then the answer is that these are reference types, and are represented like other reference type. There is a reference (a pointer) which points to a node in the heap which holds the state of the "method reference". The reference (pointer) can be held in a local variable on the stack, in a static variable, or in a field / element of some other heap-resident object.
Finally:
> I am a novice programmer, and am trying to understand properly how programs work with memory.
My advice would be don't. You don't need to understand how Java programs "work with memory" to be an effective and proficient Java programmer. The Java runtime system deals with all of that stuff ... so that you don't need to understand it.
My advice would be:
- Wait until you are an experienced Java programmer.
- Wait until you have done degree level courses on both computer hardware architectures AND compiler writing.
- Wait until you need to understand this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论