Final variable manipulation in Java

前端 未结 11 779
无人及你
无人及你 2020-11-28 23:28

Could anyone please tell me what is the meaning of the following line in context of Java:

final variable can still be manipulated unless it\'s immut

11条回答
  •  生来不讨喜
    2020-11-29 00:05

    As others have said, it means that you can manipulate the object the variable points at, but you cannot change the reference (i.e. assign another object to the variable).

    Objects that are mutable by design, such as a List can be changed (you can add elements to them) whereas if you have an immutable object such as a String or Integer you won't be able to change it (all the operations the class String supports return a new instance, and don't modify the actual object).

提交回复
热议问题