版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LeoZuosj/article/details/86669845
String和StringBuffer分别作为参数传递
-
A:形式参数问题
- String作为参数传递
- StringBuffer作为参数传递
-
B:案例演示
- String和StringBuffer分别作为参数传递问题
基本数据类型的值传递,不改变其值
引用数据类型的值传递,改变其值
String类虽然是引用数据类型,但是他当作参数传递时和基本数据类型是一样的
- String和StringBuffer分别作为参数传递问题
package com.heima.stringbuffer;
public class Demo07_StringBuffer {
public static void main(String[] args) {
String s = "heima";
System.out.println(s);
//String这个类一旦被初始化就不会被改变
//change方法调用完之后就弹栈了,hemaiitcast就消失了
//所以打印的s还是heima
change(s);
System.out.println(s);
System.out.println("---------------------");
StringBuffer sb = new StringBuffer();
sb.append("heima");
System.out.println(sb);
change(sb);
System.out.println(sb);
}
public static void change(StringBuffer sb) {
sb.append("itcast");
}
public static void change(String s) {
s += "itcast";
}
}