0
可能重複間不同:
what is the difference between String and StringBuffer in java?的StringBuffer和字符串
當我測試使用的StringBuffer和字符串的函數。當我使用StringBuffer時,我在2或3秒鐘內得到了java.lang.OutOfMemoryError。但是,當我使用String時,直到一分鐘,我纔得到java.lang.OutOfMemoryError錯誤。他們有什麼不同,我不完全清楚。
public void print() {
StringBuffer buffer = new StringBuffer();
double result = 1.0/7.0;
buffer.append(result);
while (result != 0) {
result = result % 7.0;
buffer.append(result);
}
System.out.println(buffer.toString());
}
public void print() {
String st = "";
double result = 1.0/7.0;
st = st + result;
while (result != 0) {
result = result % 7.0;
st = st + result;
}
System.out.println(st);
}
可能的重複http://stackoverflow.com/questions/2439243/what-is-the-difference-between-string-and-stringbuffer-in-java – gigadot
StringBuffer更快 – SJuan76
它會更快。我不知道爲什麼我用StringBuffer獲得內存不足。現在,我正在參考http://stackoverflow.com/questions/2439243/what-is-the-difference-between-string-and-stringbuffer-in-java – CycDemo