无论是初学者还是在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]
2.将textString定义为全局变量
[img]http://dl.iteye.com/upload/picture/pic/93360/cf43f454-d319-332a-a9b7-7beb4215b850.jpg[/img]
[color=green]上面这个全局的效果等价于:[/color]
[size=xx-large][color=red]区别就是一句话:
String引用改变指向一个新的地址,
StringBuffer引用不变指向自己本身[/color][/size]
原文:[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]