// StringBufferTest1.java
class StringBufferTest1 {
public static void main(String args[]) {
String s = "";
// StringBuffer sb = new StringBuffer();
Runtime rt = Runtime.getRuntime();
long x = rt.freeMemory();
for(int i = 0; i < 1000; i++)
s += "0";
// sb.append("0");
long y = rt.freeMemory();
System.out.println(x - y);
}
}
// StringBufferTest2.java
class StringBufferTest2 {
public static void main(String args[]) {
//String s = "";
StringBuffer sb = new StringBuffer();
Runtime rt = Runtime.getRuntime();
long x = rt.freeMemory();
for(int i = 0; i < 1000; i++)
//s += "0";
sb.append("0");
long y = rt.freeMemory();
System.out.println(x - y);
}
}
//แนะนำควรใช้ ตัวที่เป็น StringBuffer เพราะไม่เปลือง Memory
No comments:
Post a Comment