JavaSE8基础 StringBuffer toString 将其转为String对象

os :windows7 x64
    jdk:jdk-8u131-windows-x64
    ide:Eclipse Oxygen Release (4.7.0)
    
    
code:

package jizuiku0;

public class Demo10 {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer();
		sb.append("cnblog_");
		sb.append("jizuiku");

		String str = sb.toString();
		System.out.println(str);
	}
}

result:

sourceCode:

    @Override
    public synchronized String toString() {
        if (toStringCache == null) {
            toStringCache = Arrays.copyOfRange(value, 0, count);
        }
        return new String(toStringCache, true);
    }


Java优秀,值得学习。
学习资源:API手册+Java源码+清净的心地。

时间: 2024-08-29 12:59:55

JavaSE8基础 StringBuffer toString 将其转为String对象的相关文章

inputStream输入流转为String对象(将String对象转为inputStream输入流)

不得不说org.apache.commons包下有很多实用的工具类. org.apache.commons.io.IOUtils; 要将inputStream输入流转为String对象,只需使用org.apache.commons.io.IOUtils这个工具类. IOUtils.toString(inputStream):将inputStream输入流转为String对象 IOUtils.toInputStream(String string); 将String对象转为inputStream输

JavaSE8基础 Integer.toString 将int类型转为同面值的String类型

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo001 { public static void main(String[] args) { int num = 1;//int 没有 tostring方法 Integer i = new Integer(num);//Integer有

JavaSE8基础 StringBuffer与String变量在追加字符串后,引用变量的地址发生改变

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku0; public class Demo11 { public static void main(String[] args) { String str1 = "cnblog"; String str2 = str1 + "jizuiku"; S

JavaSE8基础 Integer.toString 将十进制类型转为指定进制[2,36]的表达形式

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku3; public class Demo010 { public static void main(String[] args) { System.out.println(Integer.toString(127, 2));//127的二进制 System.out.print

JavaSE8基础 StringBuffer substring 将变量分割返回String,其本身不变

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku1; public class Demo110 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("cnblog"); String

JavaSE8基础 StringBuffer 将一个char字符重复指定次数后输出

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) code: package jizuiku0; /* * @version V17.09 */ public class repeatCharDemo { public static void main(String[] args) { // 字符 0 重复输出3次 System.out.println(repeatChar('0

JavaSE8基础 StringBuffer的三种构造方法

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku0; public class Demo00 { public static void main(String[] args) { //无参构造 StringBuffer sb = new StringBuffer();//看API手册+源码可得,初始容量为16 System

JavaSE8基础 StringBuffer delete trimToSize 清空字符串缓冲区与整理缓冲区的空间

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku1; public class Demo100 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("cnblog"); System.

JavaSE8基础 StringBuffer reverse 倒置缓冲区内的字符串

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku1; public class Demo101 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); sb.append("cnblog"); sb.reve