[Java] String immutable caused an invalid modification parameters passed by reference

public class T2 {
	public static void change (String s) {
		s = "hah";
		System.out.println("s = "+s);
	}
	public static void change1 (StringBuffer sbr) {
		sbr.replace(0, sbr.length(), "kk");
	}
	public final static void main(String[] args) {
		String str = "jkl";
		change(str);
		System.out.println(str);
		StringBuffer srb = new StringBuffer("hh");
		change1(srb);
		System.out.println("srb = "+srb);
	}
}

operation result:

String type is immutable, the modification is invalid, the StringBuffer is a variable type, it is effective to modify

 

Guess you like

Origin blog.csdn.net/chenhanxuan1999/article/details/91485143