I am reading the source code of Java LinkedList, and note that the constructor of LinkedList
is like this:
public LinkedList() {
header.next = header.previous = header;
}
How much space will be allocated to this initialization, the header seems to create infinite recursion by pointing to itself.
It allocates a single node in the initialization of the header
instance variable:
private transient Entry<E> header = new Entry<E>(null, null, null);
The code in the constructor to which you refer allocates no memory; it merely sets up the pointers to an initial state. There is no "infinite recursion", because internal traversal caters for this situation.
I'm not 100% sure what you mean by "how many space", but if you mean how much space will be allocated in memory - it's just one field initially. Even though the header pointer points to itself, all pointers still only point to a single address space in memory. Mind you, I'm not even sure it will initially point anywhere, I think they will only be assigned null values at first.
A simple example.
String a = "Hello"
String b = a;
String c = b;
The String "Hello" will only occur once in the memory, even though it has several pointers to it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.