这类操作大同小异,但是,高手却能从String,StringBuilder,StringBuffer的应用场景和性能上,分析出其在JAVA编译和JVM上的实现过程差别。
我在CSDN上就看到一个高手分析过。
http://blog.csdn.net/clam_clam/article/details/6831345
http://www.cnblogs.com/dolphin0520/p/3778589.html
public class StringBufferDemo { public static void main(String[] args) { System.out.println("----------------------"); createString(new StringBuffer("hello java")); System.out.println("----------------------"); deleteString(new StringBuffer("hello java")); System.out.println("----------------------"); replaceString(new StringBuffer("hello java")); } public static void createString(StringBuffer stringBuffer) { stringBuffer.append("hello").append("java").append(520).append(true); System.out.println(stringBuffer); stringBuffer.insert(0, "heima,"); System.out.println(stringBuffer); } public static void deleteString(StringBuffer stringBuffer) { stringBuffer.delete(0, 6); System.out.println(stringBuffer); stringBuffer.deleteCharAt(1); System.out.println(stringBuffer); } public static void replaceString(StringBuffer stringBuffer) { stringBuffer.replace(6, stringBuffer.length(), "world"); System.out.println(stringBuffer); stringBuffer.setCharAt(5, ‘,‘); System.out.println(stringBuffer); stringBuffer.reverse(); System.out.println(stringBuffer); } }
时间: 2024-11-13 10:37:50