• Java_Stringbuilder和StringBuffer


    StringBuilder和StringBuffer非常类似, 均代表可变的字符串序列. 这两个类都是抽线类AbstractStringBuilder的子类, 方法几乎一样

    /******String修改引用, StringBuilder/StringBuffer修改源字符串*******/
    public static void main(String[] args) {
    	String s1 = new String("abcdef");
    	System.out.println(Integer.toHexString(s1.hashCode()));
    	System.out.println(s1);
    	
    	s1 = "hdfa";	// 新建一个字符串, 而不是改变原来字符串的内容
    	System.out.println(Integer.toHexString(s1.hashCode()));
    	System.out.println(s1);
    
    	System.out.println("###############################");
    	
    	StringBuilder sb = new StringBuilder("hahahaha");
    	
    	System.out.println(Integer.toHexString(sb.hashCode()));
    	System.out.println(sb);
    	
    	sb.setCharAt(2, 'Z');	// 改变原来字符串的内容
    	System.out.println(Integer.toHexString(sb.hashCode()));
    	System.out.println(sb);
    }
    /*
    ab199863
    abcdef
    30cab7
    hdfa
    ###############################
    3b192d32
    hahahaha
    3b192d32
    haZahaha
    */
    

    StringBuilder

    abstract class AbstractStringBuilder implements Appendable, CharSequence {
        /**
         * The value is used for character storage.
         */
        byte[] value
    // 以下代码shenglue
    }
    

    显然, 内部也是一个字符数组, 但这个数组没有像String类中用final修饰

    StringBuilder与StringBuffer区别:

    1.StringBuffer是JDK1.0版本提供的类, 线程安全, 做线程同步检查, 效率较低
    2.StringBuilder是JDK1.5版本提供的类, 线程不安全, 不做线程同步检查, 因此效率较高, 建议使用此种方法

    常用方法

    注意链式调用

    重载public StringBuilder append(...)方法可以为该StringBuilder对象添加字符串序列, 仍然返回对象自身
    public StringBuilder delet(int start, int end)可以删除从start开始到end-1为止的一段字符序列, 仍然返回自身对象
    public StringBuilder deleteCharAt(int dex)移除此序列指定位置上的字符, 仍然返回自身对象
    重载public StringBuilder insert(...)方法可以为该StringBuilder对象在指定位置插入字符序列, 仍然返回自身对象
    public StringBuilder reverse()用于将字符序列逆序, 仍然返回自身对象
    public StringBuilder toString()返回此序列中数据的字符串表示形式, StringBuilder对象转换成String对象
    和Sting类含义类似的方法:
      public int indexOf(String str)
      public int indexOf(String str, int fromIndex)
      public String substring(int start)
      public String substring(int start, int end)
      public int length()
      char charAt(int index)

    public static void main(String[] args) {
    	StringBuilder sb = new StringBuilder();
    	
    	for(int i=0; i<26; i++) {
    	// 数字转换字母方法
    		char tmp = (char)('a' + i);
    		sb.append(tmp);
    	}
    
    	System.out.println(sb);
    	System.out.println("##########################");
    	
    	// StringBuilder对象转换String对象
    	String str = sb.toString();
    	System.out.println(str);
    	System.out.println("##########################");
    	
    	sb.reverse();
    	System.out.println(sb);
    	System.out.println("##########################");
    	
    	sb.setCharAt(0, '哈');
    	System.out.println(sb);
    	System.out.println("##########################");
    // 链式调用. 核心就是该方法调用return this, 把自己返回了
    	sb.insert(1, 'Z').insert(2, 'Y').insert(3, 'B').insert(4, "哈哈");
    	System.out.println(sb);
    	System.out.println("##########################");
    	
    	System.out.println(sb.length());
    	sb.delete(5, 31).deleteCharAt(0).deleteCharAt(3);
    	System.out.println(sb);
    	
    }
    
    /*
    abcdefghijklmnopqrstuvwxyz
    ##########################
    abcdefghijklmnopqrstuvwxyz
    ##########################
    zyxwvutsrqponmlkjihgfedcba
    ##########################
    哈yxwvutsrqponmlkjihgfedcba
    ##########################
    哈ZYB哈哈yxwvutsrqponmlkjihgfedcba
    ##########################
    31
    ZYB
    */
    

    使用陷阱

    StringBuilder和StringBuffer类是对原始字符串本身操作, 可以对字符串进行修改而不产生副本拷贝或产生少量的副本, 因此可以在循环中使用

    /*************String和StringBuilder频繁修改字符串时效率测试***************/
    public static void main(String[] args) {
    /****************使用String进行字符串的拼接*******************************/
    	String str1 = "";
    
    	long num1 = Runtime.getRuntime().freeMemory();	// 获取系统剩余内存空间
    	long time1 = System.currentTimeMillis();		// 获取系统当前时间
    
    	for(int i=0; i<5000; i++) {
    		str1 = str1 + i;
    	}
    
    	long num2 = Runtime.getRuntime().freeMemory();
    	long time2 = System.currentTimeMillis();
    
    	System.out.println("String占用内存: " + (num1 - num2));
    	System.out.println("String占用时间: " + (time2 - time1));
    	
    /**************使用StringBuilder进行字符串的拼接*************************/
    	StringBuilder sb1 = new StringBuilder("");
    
    	long num3 = Runtime.getRuntime().freeMemory();
    	long time3 = System.currentTimeMillis();
    
    	for(int i=0; i<5000; i++) {
    		sb1.append(i);
    	}
    
    	long num4 = Runtime.getRuntime().freeMemory();
    	long time4 = System.currentTimeMillis();
    
    	System.out.println("StringBuilder占用内存: " + (num3 - num4));
    	System.out.println("StringBuilder占用时间: " + (time4 - time3));
    }
    
    /*
    String占用内存: 77520136
    String占用时间: 61
    StringBuilder占用内存: 524288
    StringBuilder占用时间: 1
    */
    
  • 相关阅读:
    ohmyzsh
    https://github.com/
    init 0,1,2,3,4,5,6
    关于反射
    docker学习笔记
    docker常见问题汇总
    ArrayList的sublist
    java-锁
    CAS-原子操作
    hashMap与concurrentHashMap
  • 原文地址:https://www.cnblogs.com/hesper/p/9734135.html
Copyright © 2020-2023  润新知