Why do I need the final def in the poll () method of LinkedList in Java

/**
 * Retrieves and removes the head (first element) of this list.
 *
 * @return the head of this list, or {@code null} if this list is empty
 * @since 1.5
 */
public E poll() {
    final Node<E> f = first; //why final here? <<-------- ******* --------
    return (f == null) ? null : unlinkFirst(f);
}

Hi, I am reading the JDK 1.7 source code. In the above code snippet in LinkedList.java, I can’t understand why I need β€œfinal” in the poll () method. Why not:

public E poll() {
    return (first == null) ? null : unlinkFirst(first);
}

Can you share an understanding of the implementation? Thank.

+4
source share
3 answers

Most methods on LinkedList use declarations finalfor local variables.

LinkedList JDK 1.7 Source

This is probably due to the concept of using the final "modifier, when applicable in java .

, , , ( , ) , . , , .

, 6 , , , , .

? . , . .

final , , util, , Java , , .

. , , .

+3

concurrency-interest :

jsr166-land , Java-. - jit ( , -).

final , . ,

final Foo foo = this.foo;

+3

Compass , . , -, f , , , first. - , . , , , .

, , Java-, .

+2

Source: https://habr.com/ru/post/1568817/


All Articles