String和StringBuffer类型数据进行参数传递问题

1.String和StringBuffer的形的参做为传递
通常参数传递有两种:

                基本类型:不会影响实际参数的值

                引用类型:会影响实际参数的值

而字符串和StringBuffer的的作为引用类型是否是上面的一样的呢?首先看一下下面的例子

package xfcy_01;
/**
 * 案例: String和StringBuffer做为形参传递
 * 形式参数:
 * 		基本类型:不会影响实际参数的值
 * 		引用类型:会影响实际参数的值
 * 注意:String类型数据应该按照基本数据处理
 * @author 晓风残月
 *
 */
public class StringBufferDemo04 {
	public static void main(String[] args) {
		//1.String类型
		String str1="hello";
		String str2="world";
		System.out.println(str1+"----------------"+str2);
		change(str1,str2);
		System.out.println(str1+"----------------"+str2);
		
		//2.StringBuffer类型
		StringBuffer sb1=new StringBuffer("hello");
		StringBuffer sb2=new StringBuffer("world");
		System.out.println(sb1+"-----------------"+sb2);
		change2(sb1,sb2);
		System.out.println(sb1+"-----------------"+sb2);
	}
 
	private static void change2(StringBuffer sb1, StringBuffer sb2) {
		sb1=sb2;
		sb2=sb1.append(sb2);
	}
 
	public static void change(String str1, String str2) {
		str1=str2;
		str2=str1+str2;
	}
}

得到的结果为:

 

讲解过分析如下:

但若为的StringBuffer的类型的数据

2.总结

       在引用类型中应该注意字符串类型的数据是一种特殊情况,不会因为形参改变从而改变实参

 

       本文转载自:https://blog.csdn.net/xiaofei__/article/details/53007009

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值