char string 区别 package test; public class charOrString { public static void main(String[] args) { char a = 55; System.out.println(a += 10); String b = "55" ; System.out.println(b+=10); int c = 55 ; System.out.println(c += 10 ); } } 结果是 A551065 性能比较 package com.bovy.test.perform; /** *//** * 测试向StringBuilder中插入字符和字符串的性能区别。 这个性能区别主要是字符串需要创建类造成的。 * 在设置StringBuilder的容量足够大的情况下,性能有所改善。注意,这个容量只有在第一次容量不足的时候才调整。 * * @author limingjun * */ public class PerformTest ...{ /** *//** * @param args */ public static void main(String[] args) ...{ PerformTest performTest = new PerformTest(); performTest.testStringBuilder(); performTest.testStringBuilder(10240); performTest.testStringBuffer(); performTest.testStringBuffer(10240); } /** *//** * 在不设置容量的情况下。消耗的时间: 2828 比 5761,基本类型还是快。 */ public void testStringBuffer() ...{ StringBuffer hql = new StringBuffer(); /** *//** * 在设置StringBuilder的容量足够大的情况下,性能有所改善。注意,这个容量只有在第一次容量不足的时候才调整。 * StringBuilder hql = new StringBuilder(10240); * */ long begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(' '); } } // 这个容量只有在第一次容量不足的时候才调整。 hql = new StringBuffer(); System.out.println(System.currentTimeMillis() - begin); begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(" "); } } System.out.println(System.currentTimeMillis() - begin); } /** *//** * 在设置StringBuilder的容量足够大的情况下,性能有所改善。注意,这个容量只有在第一次容量不足的时候才调整。 消耗的时间: 1670 比 * 4409,基本类型还是快。 * * @param capacity */ public void testStringBuffer(int capacity) ...{ StringBuffer hql = new StringBuffer(capacity); long begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(' '); } } // 这个容量只有在第一次容量不足的时候才调整。 hql = new StringBuffer(capacity); System.out.println(System.currentTimeMillis() - begin); begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(" "); } } System.out.println(System.currentTimeMillis() - begin); } /** *//** * 在不设置容量的情况下。消耗的时间: 1578 比 4559,基本类型还是快。 */ public void testStringBuilder() ...{ StringBuilder hql = new StringBuilder(); /** *//** * 在设置StringBuilder的容量足够大的情况下,性能有所改善。注意,这个容量只有在第一次容量不足的时候才调整。 * StringBuilder hql = new StringBuilder(10240); * */ long begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(' '); } } // 这个容量只有在第一次容量不足的时候才调整。 hql = new StringBuilder(); System.out.println(System.currentTimeMillis() - begin); begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(" "); } } System.out.println(System.currentTimeMillis() - begin); } /** *//** * 在设置StringBuilder的容量足够大的情况下,性能有所改善。注意,这个容量只有在第一次容量不足的时候才调整。 消耗的时间: 1670 比 * 4409,基本类型还是快。 * * @param capacity */ public void testStringBuilder(int capacity) ...{ StringBuilder hql = new StringBuilder(capacity); long begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(' '); } } // 这个容量只有在第一次容量不足的时候才调整。 hql = new StringBuilder(capacity); System.out.println(System.currentTimeMillis() - begin); begin = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) ...{ hql.setLength(0); for (int j = 0; j < 10000; j++) ...{ hql.append(" "); } } System.out.println(System.currentTimeMillis() - begin); } } 原文http://heisetoufa.javaeye.com/blog/227680