Java - String and array references

前端 未结 4 1158
星月不相逢
星月不相逢 2021-01-22 18:15

Just started to learn Java, and saw that both string and array are reference types. I don\'t understand the following issue:

    String a = \"a1\";
    String b          


        
4条回答
  •  深忆病人
    2021-01-22 18:30

    In the array case, you're actually modifying the array, and thus if one reference is changed, so is the other.

    In the string case, you are not modifying the object, you are simply assigning a different object to that reference. As you noted: a=b means "let a point to the same object as b is pointing". Following the same line of thought, a="rrr" means "let a point to the literal "rrr"", which has nothing to do with with b.

提交回复
热议问题