String StringBuffer StringBuilder
String http://docs.oracle.com/javase/7/docs/api/
中文: http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html
String str0 = "最正常的创建方法"; System.out.println(str0); char[] c1 = new char[3]; c1[0] = 'c'; c1[1] = 'b'; c1[2] = 'a'; String str1 =new String("string1"); String str2 = new String(c1); System.out.println(str1+ " "+ str2); //输出string1 cba /* String.valueOf()它可以将JAVA基本类型(int,double,boolean等)和对象(Object)转换成String型 * toString()是对象的方法,它可以将该对象转换成String型,转换算法根据类型实际需要而定, * 基本上JAVA里面每个对象都会有一个toString方法。*/ //字符串转换 int i1 = 100; String str3= Integer.toString(i1); System.out.println("Integer.toString : "+str3);//输出Integer.toString : 100 //valueOf()进行数据转换 String str32 = String.valueOf(123456); System.out.println("String.valueOf() : " +str32);//输出String.valueOf() : 123456 //字符抽取 //charAt String str4 = "A long String!"; System.out.println("charAt(3) : "+str4.charAt(3));//输出charAt(3) : o //getChars() char buf [] = new char[6-2]; str4.getChars(2, 6, buf , 0); System.out.print("getChars输出 :"); System.out.println(buf); //输出getChars输出 :long //char[] toCharArray() System.out.println(str4.toCharArray());//输出A long String! //抽取子串 String str5 = str4.substring(2,8); System.out.println("subString(2,8)输出 : "+str5);//输出subString(2,8)输出 : long S
StringBuffer http://docs.oracle.com/javase/7/docs/api/
A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified.
/* * StringBuffer() * StringBuffer(int size) * StringBuffer(String str) * 接收一个String变元,指定StringBuffer对象的初始内容,并额外预留16个字符空间 * StringBuffer(CharSequence chars) */ StringBuffer sbf = new StringBuffer("Hello"); System.out.println("buffer = " +sbf); System.out.println("length = " +sbf.length()); System.out.println("capacity = " +sbf.capacity()); System.out.println("----重新指定缓冲区大小----"); sbf.ensureCapacity(55); System.out.println("buffer = " +sbf); System.out.println("length = " +sbf.length()); System.out.println("capacity = " +sbf.capacity()); System.out.println("----设置缓冲区长度----"); sbf.setLength(30); System.out.println("buffer = " +sbf); System.out.println("length = " +sbf.length()); System.out.println("capacity = " +sbf.capacity());
输出结果:
buffer = Hello length = 5 capacity = 21 ----重新指定缓冲区大小---- buffer = Hello length = 5 capacity = 55 ----设置缓冲区长度---- buffer = Hello
length = 30
capacity = 55
StringBuilder http://docs.oracle.com/javase/7/docs/api/
除了一个重要的区别之外,StringBuffer等同于StringBuilder,区别是:它不是同步的,即意味着它不是线程安全的。
StringBuilder优势在于具有更快的性能,但是使用多线程时必须使用StringBuffer,而不能使用StringBuilder