What is the difference between immutable and final in java?

前端 未结 9 1939
北恋
北恋 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:43

    Immutable:

    when you change the String, create new String object ('abcdef') and change the reference from 'abce' to 'abcdef'.But you can not remove 'abcd'. Only change the reference. That is immutable.

    final:

    Actually final is a keyword.When you add it to variable, you can not change the reference.

提交回复
热议问题