What is meant by immutable?

后端 未结 17 1640
天命终不由人
天命终不由人 2020-11-22 02:27

This could be the dumbest question ever asked but I think it is quite confusing for a Java newbie.

  1. Can somebody clarify what is meant by immutable? <
17条回答
  •  鱼传尺愫
    2020-11-22 03:01

    Immutable means that once the object is created, non of its members will change. String is immutable since you can not change its content. For example:

    String s1 = "  abc  ";
    String s2 = s1.trim();
    

    In the code above, the string s1 did not change, another object (s2) was created using s1.

提交回复
热议问题