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 str = sb.substring(3,5);//返回的是String [3,5)
		System.out.println(str);//截取部分
		System.out.println(sb);//本身,为什么本身不变呢?看源码!
	}
}

result:

sourceCode:

  第一级

    @Override
    public synchronized String substring(int start) {
        return substring(start, count);
    }

  点击return后的 substring,进入第二级

	@Override
    public synchronized String substring(int start, int end) {
        return super.substring(start, end);
    }

  点击super.后面的 substring,进入第三级

	public String substring(int start, int end) {
        if (start < 0)
            throw new StringIndexOutOfBoundsException(start);
        if (end > count)
            throw new StringIndexOutOfBoundsException(end);
        if (start > end)
            throw new StringIndexOutOfBoundsException(end - start);
        return new String(value, start, end - start);
    }

  奥,原来是这样的,懂了!



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

时间: 2024-11-06 07:23:04

JavaSE8基础 StringBuffer substring 将变量分割返回String,其本身不变的相关文章

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基础 StringBuffer append 向缓冲区中变量的末尾追加字符串与缓冲区容量的自动扩充

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku1; public class Demo000 { public static void main(String[] args) { StringBuffer sb = new StringBuffer(); System.out.println("缓冲容的容量是:"

JavaSE8基础 链式编程 调用方法返回对象再调方法 简单示例

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        代码: class Person { public void sayHello() { System.out.println("hello world"); } } class Tools { public static Person getPersonObj() { return new Person()

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 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

JavaSE8基础 接口的成员变量只能是静态的常量

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        代码: //接口的成员变量只能是静态的常量. interface PersonTrain{ //默认修饰符public static final public static final int num = 1;//推荐写法,帮人帮己 public int num2 = 2; public final int num3

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.appe

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