String
1,Stirng是对象不是基本数据类型 2,String是final类,不能被继承。是不可变对象,一旦创建,就不能修改它的值。 3,对于已经存在的Stirng对象,修改它的值,就是重新创建一个对象,然后将新值赋予这个对象 StringBuffer 1,一个类似于 String 的字符串缓冲区,对它的修改的不会像String那样重创建对象。 2,使用append()方法修改Stringbuffer的值,使用toString()方法转换为字符串。 Stringbuild 是jdk1.5后用来替换stringBuffer的一个类,大多数时候可以替换StringBuffer。和StringBuffer的区别在于Stringbuild是一个单线程使用的类,不值执行线程同步所以比StringBuffer的速度快,效率高。是线程非安全的。下面举个举个例子:
package com.pingan.test;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class StringBuilderTester { private static final String base = " base string. "; private static final int count = 2000000; /** * Sting是字符串不可变的(常量),String拼接每次都会新实例化一个对象 */ public static void stringTest() { long begin, end; begin = System.currentTimeMillis(); String test = new String(base); for (int i = 0; i < count / 100; i++) { test = test + " add "; } end = System.currentTimeMillis(); System.out.println((end - begin) + " millis has elapsed when used String. "); } /** * StringBuffer是字符串长度是可变,是线程安全的,线程同步保证线程安全而导致性能下降 */ public static void stringBufferTest() { long begin, end; begin = System.currentTimeMillis(); StringBuffer test = new StringBuffer(base); for (int i = 0; i < count; i++) { test = test.append(" add "); } end = System.currentTimeMillis(); System.out.println((end - begin) + " millis has elapsed when used StringBuffer. "); } /** * StringBuilder是字符串可变的,是线程不安全的,比StringBuffer处理效率高 */ public static void stringBuilderTest() { long begin, end; begin = System.currentTimeMillis(); StringBuilder test = new StringBuilder(base); for (int i = 0; i < count; i++) { test = test.append(" add "); } end = System.currentTimeMillis(); System.out.println((end - begin) + " millis has elapsed when used StringBuilder. "); } public static String appendItemsToStringBuiler(List list) { StringBuilder b = new StringBuilder(); for (Iterator i = list.iterator(); i.hasNext();) { b.append(i.next()).append(" "); } return b.toString(); } public static void addToStringBuilder() { List list = new ArrayList(); list.add(" I "); list.add(" play "); list.add(" Bourgeois "); list.add(" guitars "); list.add(" and "); list.add(" Huber "); list.add(" banjos "); System.out.println(StringBuilderTester.appendItemsToStirngBuffer(list)); } public static String appendItemsToStirngBuffer(List list) { StringBuffer b = new StringBuffer(); for (Iterator i = list.iterator(); i.hasNext();) { b.append(i.next()).append(" "); } return b.toString(); } public static void addToStringBuffer() { List list = new ArrayList(); list.add(" I "); list.add(" play "); list.add(" Bourgeois "); list.add(" guitars "); list.add(" and "); list.add(" Huber "); list.add(" banjos "); System.out.println(StringBuilderTester.appendItemsToStirngBuffer(list)); } public static void main(String[] args) { stringTest(); stringBufferTest(); stringBuilderTest(); addToStringBuffer(); addToStringBuilder(); }}
运行的结果:
1143 millis has elapsed when used String. 157 millis has elapsed when used StringBuffer. 94 millis has elapsed when used StringBuilder. I play Bourgeois guitars and Huber banjos I play Bourgeois guitars and Huber banjos