不要再困扰在String与StringBuffer(StringBuilder)的区别上鸟

无论是初学者还是在java这条路上游荡了几年的童鞋都会每每想起String与StringBuffer的区别这个话题!今天再次看见这个话题,该是终结的时候咯!

原文:[url=http://www.iteye.com/topic/142364#422534]http://www.iteye.com/topic/142364#422534[/url]


看原文后我的总结:

java常量池:[url=http://wenku.baidu.com/view/4a2f961c59eef8c75fbfb348.html]http://wenku.baidu.com/view/4a2f961c59eef8c75fbfb348.html[/url]

[color=red]注意为对象在堆中编写的地址变化以及不变化哦[/color]
.
1.将textString定义为局部变量

[img]http://dl.iteye.com/upload/picture/pic/93362/c65a09db-b4e2-3e7a-abd7-4051b0529f4a.jpg[/img]


public class testStringStringBuffer {
public static void stringReplace (String text) {
text = text.replace('j' , 'i');
System.out.println (text);
}

public static void bufferReplace (StringBuffer text) {
text = text.append("C");
}

public static void main (String args[]) {
String textString = new String ("java");
StringBuffer textBuffer = new StringBuffer ("java");

stringReplace (textString);
bufferReplace (textBuffer);

System.out.println (textString + textBuffer);
}

}




输出结果:
iava
javajavaC


2.将textString定义为全局变量

[img]http://dl.iteye.com/upload/picture/pic/93360/cf43f454-d319-332a-a9b7-7beb4215b850.jpg[/img]



/**
*@source:http://www.iteye.com/topic/142364#422534
*
*@function:
*
* @author ocaicai@yeah.net 2011-6-29
*
*/
public class testStringStringBuffer {

static String textString = null;// 将textString定义为全局变量

public static void stringReplace(String text) {
//让textString指向在堆中新产生的对象
textString = text.replace('j', 'i');
System.out.println(textString);
}

public static void bufferReplace(StringBuffer text) {
text = text.append("C");
}

public static void main(String args[]) {
textString = new String("java");
StringBuffer textBuffer = new StringBuffer("java");

stringReplace(textString);
bufferReplace(textBuffer);

System.out.println(textString + textBuffer);
}

}



输出结果:
iava
iavajavaC


[color=green]上面这个全局的效果等价于:[/color]



package com.cdl.mix;

/**
*@source:http://www.iteye.com/topic/142364#422534
*
*@function:
*
* @author ocaicai@yeah.net 2011-6-29
*
*/
public class testStringStringBuffer {

//static String textString = null;// 将textString定义为全局变量

// public static void stringReplace(String text) {
// //让textString指向在堆中新产生的对象
// textString = text.replace('j', 'i');
// System.out.println(textString);
// }

public static void bufferReplace(StringBuffer text) {
text = text.append("C");
}

public static void main(String args[]) {
String textString = new String("java");
StringBuffer textBuffer = new StringBuffer("java");

//stringReplace(textString);
textString=textString.replace('j', 'i');
System.out.println(textString);
bufferReplace(textBuffer);

System.out.println(textString + textBuffer);
}

}





输出结果:
iava
iavajavaC



[size=xx-large][color=red]区别就是一句话:

String引用改变指向一个新的地址,

StringBuffer引用不变指向自己本身[/color][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值