例如,如果我上一个类:
class Node
{
int myvalue;
Node myparent;
}
节点“myparent”是对节点的引用,还是该节点的副本?如果它是对节点的引用,这意味着内存量将大约与我拥有的节点总数(包括它们的值)一样多,对吗?或者节点是节点的副本,因此所有节点占用的内存与其深度*(节点占用的内存量)一样多?
最佳答案
Java中所有非原始变量都是引用(实际上是指针)。从文档中可以清楚地看出这一点:
There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1
The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.
http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3.1
关于Java:本身的类和内存使用情况(例如:简单的二叉树实现)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458754/