Exploring the code of java.util.LinkedList as it is in OpenJDK 8, I found the following code. The code is straightforward, but I'm confused about saving reference to the first node to a constant in the second line of code. As far as I understand, this code will be inlined to one-liner without reference copying. Am I right? If so, why does one need to copy reference in this and similar situations (such idiom may be found in a half of methods in java.util.LinkedList)?
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
My first thought was that it helps concurrency in some way, but LinkedList does not allow concurrent access (except at your own risk), so I guess it is some hint for optimizer, but can't figure out how it is supposed to work.