final变量的好处

J2EE 码拜 8年前 (2016-01-31) 844次浏览
例如一段LinkedList里面的代码
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
加个final有什么好处?
写成这样不就行了:
public E getFirst() {
if (first == null)
throw new NoSuchElementException();
return first.item;
}
还有一些代码喜欢在catch(final Exception e)里面加final,又有什么内幕?

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明final变量的好处
喜欢 (0)
[1034331897@qq.com]
分享 (0)