Immutability of Strings in Java

后端 未结 26 2380
不思量自难忘°
不思量自难忘° 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条回答
  •  别那么骄傲
    2020-11-21 07:37

    Regarding the replace part of your question, try this:

    String str = "Mississippi"; 
    System.out.println(str); //Prints Mississippi 
    
    String other = str.replace("i", "!"); 
    System.out.println(str); //still prints Mississippi 
    System.out.println(other);  // prints M!ss!ss!pp!
    

提交回复
热议问题