What is the difference between immutable and final in java?

前端 未结 9 1850
北恋
北恋 2021-01-31 08:51

I was recently asked this quesion. But was not able to explain concisely what exactly sets both these concepts apart.

For example

Final and Immutable:<

9条回答
  •  遥遥无期
    2021-01-31 09:44

    When you have a field declared as final, the reference will not change. It will always point at the same Object.

    if the Object is not immutable, the methods on it can be used to change the Object itself - it is the same Object, but its properties have been changed.

提交回复
热议问题