13.字符串(1)

13.1  字符串是不可以变的

public class Demo {
	public static void main(String[] args) {
		String q = "hello";
		System.out.println(q);
		String qq = upcase(q);
		System.out.println(q);
		System.out.println(qq);
	}
	
	public static String upcase(String s) {
		return s.toUpperCase();
	}
}
hello
hello

HELLO

总结:

1.每当把String 类作为方法参数的时候,都会复制一份引用,而该引用所指的对象其实一直在同一个位置。

2.当upcase运行时,s引用才存在,运行结束,s消失。

3.返回的引用指向了一个新的对象,原本的q没有做任何改变。

4.对于一个方法而言,参数是为该方法提供信息的,而不是想让方法改变自己。


猜你喜欢

转载自blog.csdn.net/perfect_red/article/details/80330111
今日推荐