Immutability of Strings in Java

后端 未结 26 2952
不思量自难忘°
不思量自难忘° 2020-11-21 06:33

Consider the following example.

String str = new String();

str  = \"Hello\";
System.out.println(str);  //Prints Hello

str = \"Help!\";
System.out.println(s         


        
26条回答
  •  萌比男神i
    2020-11-21 07:24

    The string object that was first referenced by str was not altered, all that you did was make str refer to a new string object.

提交回复
热议问题