package com.xiaojiang.test;
public class Test {
//一个简单的例子来说明String和StringBuilder是值传递还是引用传递
public static void main(String[] args) {
String str = "jiangshijun";
changeValue(str);
System.out.println(str);
StringBuilder sb = new StringBuilder();
sb.append("xiaojiang");
changeValueSB(sb);
System.out.println(sb.toString());
}
public static void changeValue(String str){
str = "hello"+str;
}
public static void changeValueSB(StringBuilder str){
str.append(" nihao");
}
//结果:
// jiangshijun
// xiaojiang nihao
}
最后发现String的值并没有被改变!
简单的测试一下String和StringBuilder是值传递还是引用传递
最新推荐文章于 2022-03-12 13:49:23 发布