Bootstrap

Java String类的常用方法(String值能否改变?)

·String 是引用类型数据,其值是无法改变的;

public class Demo {
	public static void main(String[] args) {
		String str="Hello";
		fun(str);
		System.out.println(str);
	}
	public static void fun(String temp) {
		temp="World";
	}

}


==============分割线===============

看似能改变String的值其实是指向改变

class Message {
	private String info = "Hello";

	public Message(String info) {
		this.info = info;
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

}

public class Demo {
	public static void main(String[] args) {
		Message msg = new Message("你好");
		fun(msg);
		System.out.println(msg.getInfo());
	}

	public static void fun(Message temp) {
		temp.setInfo("World");
	}

}

;