0

If I make a class, for instance:

class Node
{
    int myvalue;
    Node myparent;
}

Is the Node "myparent" going to be a reference to a node, or is it going to be a copy of that node? If it is a reference to the node, this means that the amount of memory will be about as much as the total # of Nodes I have (including their values), correct? Or is the Node a copy of a Node, and therefore all Nodes take up as much memory as their depth*(amount of memory a node takes)?

2 Answers2

1

First of all Node myParent is a reference to Object. A reference is basically an address where object variables and methods are stored. It is not a copy of that node. And yes, the amount of memory will be same as the total number of Nodes you create.

You can read more about it here

Community
  • 1
  • 1
Yogesh D
  • 1,558
  • 14
  • 29
1

All non-primitive variables in Java are references (which are actually pointers). This is clear from the documentation:

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

Lew Bloch
  • 3,364
  • 1
  • 16
  • 10