Java 常用类 于 StringBuffer 和 StringBuilder的使用 + String三者的异同

  1 package com.bytezero.stringclass;
  2 
  3 import org.junit.Test;
  4 
  5 /**
  6  * 关于 StringBuffer 和 StringBuilder的使用
  7  *
  8  *
  9  *
 10  *
 11  * @author Bytezero1·zhenglei!    Email:420498246@qq.com
 12  * create  2021-10-22 17:57
 13  */
 14 public class StringBufferBuildeTest {
 15     /*
 16 
 17     对比String,StringBuffer,StringBuilder三者的效率:
 18     从高到底排列:StringBuilder > StringBuffer > String
 19      */
 20 
 21 
 22 
 23      /*
 24 
 25             StringBuffer的常用方法
 26      StringBuffer append(xxx):提供了很多的append()方法,用于进行字符串拼接
 27      StringBuffer delete(int start,int end):删除指定位置的内容
 28      StringBuffer replace(int start,int end,String str): 把[start,end]位置替换为str
 29      StringBuffer insert(int offset,xxx):在指定位置插入xxx
 30      StringBuffer reverse(): 把当前字符序列逆转
 31 
 32      public int indexOf(String str)
 33      public String subString(int start,int end)
 34      public int Length()
 35      public char charAt(int n)
 36      public void setCharAt(int n,char ch)
 37 
 38        总结:
 39        增 : append(xxx)
 40        删   delete(int start,int end)
 41        改   setCharAt(int n,char ch)
 42        查   charAt(int n)
 43        插   insert(int offset,xxx)
 44        长度 Length()
 45        遍历  for +  charAt(); / toString
 46 
 47 
 48       */
 49     @Test
 50     public  void  test2(){
 51 
 52         StringBuffer s1 = new StringBuffer("abc");
 53         s1.append(1);
 54         s1.append('1');
 55         System.out.println(s1); //abc11
 56 
 57 //        s1.delete(2,4);     //ab1
 58 //        s1.replace(2,4,"zhenglei"); //abzhenglei1
 59 //        s1.insert(2,false);
 60 
 61 //        s1.reverse();  //11cba
 62         String s2 = s1.substring(1, 3);
 63         System.out.println(s2); //bc
 64 
 65         System.out.println(s1);  //abfalsec11
 66         System.out.println(s1.length());
 67 
 68 
 69     }
 70 
 71 
 72 
 73 
 74     /*
 75     String,StringBuffer,StringBuilder三者的异同?
 76 
 77     String: 不可变的字符序列   底层使用char[]存储
 78     StringBuffer:可变的字符序列;    线程安全,效率低  底层使用char[]存储
 79     StringBuilder:可变的字符序列    jdk 5.0 效率高  线程不安全 底层使用char[]存储
 80 
 81     源码分析:
 82     String str = new String();        //char[] value = new char[0];
 83     String str1 = new String("abc");  //char[] value = new char[]{'a','b','c'};
 84 
 85     StringBuffer sb1 = new StringBuffer(); //char[] value = new char[16];底层创建了一个长度是16的数组
 86     System.out.println(sb2.length());    //0
 87      sb1.append('a');  //value[0] = 'a'
 88      sb1.append('b');  //value[1] = 'b'
 89 
 90      StringBuffer sb2 = new StringBuffer("abc"); //char[] value = new char["abc".length()+16];
 91 
 92      //问题一 System.out.println(sb2.length());  //3
 93      //问题二 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组
 94               默认情况下,扩容为原来容量的 2 倍 + 2,同时将原有的数组中的元素复制到新的数组中
 95 
 96          开发中 建议大家使用:StringBuffer(int capacity) 或StringBuid(int capacity)
 97 
 98 
 99 
100 
101     注意:作为参数传递的话,方法内部String不会改变其值,StringBuffer和StringBuilder
102     会改变其值
103 
104 
105      */
106 
107     @Test
108     public  void test1(){
109 
110         StringBuffer sb1 = new StringBuffer("abc");
111         sb1.setCharAt(0,'m');
112         System.out.println(sb1);  //mbc
113 
114         StringBuffer sb2 = new StringBuffer();
115         System.out.println(sb2.length());   //0
116 
117         StringBuffer sb3 = new StringBuffer("abc");
118         System.out.println(sb3.length());  //3
119 
120        StringBuffer s3 =   new StringBuffer(20);
121 
122 
123 
124     }
125 }

 

posted on 2021-10-22 22:39  Bytezero!  阅读(58)  评论(0编辑  收藏  举报