aand bare references to the same array (in memory there is one Array object).
a ---> ["a", "b", "c"] <---- b
You change this array value with this line:
a[0] = "Z"
So you know this in memory:
a ---> ["Z", "b", "c"] <---- b
For strings this is different.
First, you have two variables indicating the same value:
String s1 = "hello";
String s2 = s1;
You have this in mind:
s1 ---> "hello" <---- s2
But then you assign s1 to the new value with this code:
s1 = "world";
s2 "". 2 .
s1 ---> "world"
s2 ---> "hello"
Java , .
. .
, , .
public class Foo() {
private int _bar = 0;
public void setBar(int bar) {
this._bar = bar
}
public void getBar() {
return this._bar;
}
}
Foo f1 = new Foo();
Foo f1 = f2;
:
f1 ----> Foo [ _bar = 0 ] <---- f2
:
f1.setBar(1)
f2.setBar(2)
- "" :
f1 ----> Foo [ _bar = 2 ] <---- f2
f2 , :
f2 = new Foo();
,
.
f1 ----> Foo [ _bar = 2 ]
f2 ----> Foo [ _bar = 0 ]