Regarding the implementation issues of java collection LinkedList
过去多啦不再A梦
过去多啦不再A梦 2017-06-23 09:14:09
0
2
938
public E removeLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return unlinkLast(l);
}

last is a member variable. Why not use it directly in the method, but assign it to a final local variable?

过去多啦不再A梦
过去多啦不再A梦

reply all(2)
typecho

Looked at the code

    transient Node<E> last;

last is transient, right? If you assign it to a local final variable, you don’t need to check the value every time you use last, right?

巴扎黑

Part of the reason is to ensure thread safety. Assuming that this method does not use the l variable but directly references the last member, then the judgment becomes if(this.last == null). If last is assigned to null immediately after passing the judgment, then the next sentence unlinkLast(this.last) will have an unknown result.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!