Jos*_*Lee 31
在Java中,术语final指的是引用,而immutable指的是对象.将final
修饰符分配给引用意味着它不能更改为指向另一个对象,但如果对象本身是可变的,则可以对其进行修改.
例如:
final ArrayList<String> arr = new ArrayList<String>();
arr.add("hello"); // OK, the object to which arr points is mutated
arr = null; // Not OK, the reference is final and cannot be reassigned to
Run Code Online (Sandbox Code Playgroud)
正如维基百科的文章所提到的,如果你来自C++,你必须分离const
成为final
和不可变的概念.