Java - string immutability and array mutability

I know that the array aand bpoint to the same place where the line s1, s2- no. What for?

code:

    String[] a = {"a","b","c"};
    String[] b = a;
    a[0] = "Z";
    String s1 = "hello";
    String s2 = s1;
    s1 = "world";
    System.out.println(Arrays.toString(a) + " - a"); //a and b are same
    System.out.println(Arrays.toString(b) + " - b");
    System.out.println(s1 + " "+ s2); // s1 and s2 are not same.

Output:

    [Z, b, c] - a
    [Z, b, c] - b
    world hello
+4
source share
4 answers

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) // This is the same object

- "" :

f1 ----> Foo [ _bar = 2 ] <---- f2

f2 , :

f2 = new Foo();

, .

f1 ----> Foo [ _bar = 2 ] 
f2 ----> Foo [ _bar = 0 ]
+2

(2), s1 s2 . s1, , .

String s1 = "hello";
String s2 = s1; (2)
s1 = "world";

,

s1 == s2

, equals, . , s1 , :

+-------+
| world |  <- s1
+-------+

+-------+
| hello |  <- s2
+-------+
+5

- , , , , , a, b.

, , s1 = "world".

:

:

String s1 = "hello"; //creates a new String object with value "hello" and assigns a reference to that object to s1
String s2 = s1;  //s2 now points to the same object as s1
s1 = "world"; //creates a new String object with value "world" and assigns a reference to that object to s1

, s1 "world".

:

String[] a = {"a","b","c"}; //creates the array and assigns a reference to it to variable a
String[] b = a; //copies the reference of a to b, so both point to the same object
 a[0] = "Z"; //changes the 1st element in the array, a and b still point to it

a, , 1- , "Z".

+1

.

(x = ...) , , , , .

(x.something = ... x[...] = ...) , , , .

, - , , ( ) , ( ).


, , , .

Many people can point to the same book. You can force someone to point to another book without changing the book itself or what someone else points to. If you write something in a book, someone will point to everyone pointing to this book, you will see the changes.

int[] a = {0,1,2,3,4}; // Assign a to, let say, "Object 1"
int[] b = {5,6,7,8};   // Assign b to, let say, "Object 2"
int[] c = a;           // Assign c to Object 1
a[0] = 2;              // Change Object 1 0th element
assert c[0] == 2;      // c points to Object 1, whose 0th element is now 2
a = b;                 // Assign a to Object 2
assert c[0] == 2;      // c still points to Object 1
+1
source

Source: https://habr.com/ru/post/1607651/


All Articles