Home > Back-end >  String is immutable and use the final decoration immutable array address how to understand
String is immutable and use the final decoration immutable array address how to understand

Time:04-13


Final char value []={' a ', 'b', 'c'};
Final char value2 []={' g '};
Value2=value;

the above value and value2 use final modification is immutable, if you have the address of the value assigned to value2 complains so, so why can the following code
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
String a="a".
A="b".
System.out.println(a);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

String instead of using the bottom of the final modify the value, so value this address cannot be changed, so the above why also established
/* private final char value []; */this is a String array implementation bottom (Java8)

CodePudding user response:

Array variable declarations for the final, namely variable reference target immutable, but the value of the various elements of the reference target can be modified, so arrays value2 and the value of the reference target is not can change, but the value [0]='f' is set up,
Variable a quoted string content has not changed, is still "a"
But will the cited is assigned to a "b", then a can reference to "b",
  • Related