第五条 避免创建不必要的对象
第五条 避免创建不必要的对象
(1)
String s = new String("S");String s1 = "s";
long l1 = new Date().getTime();for(int i = 0; i<1000000; i++) {String s = new String("S");}long l2 = new Date().getTime();System.out.println(l2 - l1);long l3 = new Date().getTime();String s1;for(int i = 0; i<1000000; i++) {s1 = "S";}long l4 = new Date().getTime();System.out.println(l4 - l3);
package com.util;public class Test {public static void main(String[] args) {StringBuilder sd = new StringBuilder();sd.append("H").append("ello");String s = "World";sd.append(s);System.out.println(sd);StringBuffer sb = new StringBuffer();sb.append("H").append("ello");String s1 = "World";sb.append(s1).toString();System.out.println(sd);}}
package com.util;public class Test {public static void main(String[] args) {Long sum = 0L;for(long i = 0; i<Integer.MAX_VALUE; i++) {sum +=i;}}}