String,StringBuffer,StringBuilder性能比较,线程安全测试,源码解析。

  1. 第一个区别

public class example1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String string="aaa";
        String string1="aaa";
        String str=new String("abc");
        String str1=new String("abc");
        StringBuffer strbuf=new StringBuffer("bcd");
        StringBuffer strbuf1=new StringBuffer("bcd");
        StringBuilder strbui=new StringBuilder("cde");
        StringBuilder strbui1=new StringBuilder("cde");

        if (string==string1) {
            System.out.println("string==string1");
        }
        if (str==str1) {
            System.out.println("str和str1 ==相同");
        }
        if (strbuf==strbuf1) {
            System.out.println("strbuf和strbuf1 ==相同");
        }
        if (strbui==strbui1) {
            System.out.println("strbui和strbui1 ==相同");
        }


    }

}

结果string==string1

  1. 解析:==比较的是地址,结果表明String是个常量,“abc”在内存中创建,这里的string,string1都是指向的”abc”,所以相等,而其它都是创建新的对象,所以地址不同,不相等。StringBuffer,StringBuilder都没有重写equals方法,所以是继承的object方法,比较的是地址。
  2. 性能比较
package test;

public class example {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       String str="abc";      
       long starttime=System.currentTimeMillis();    
       for (int i = 0; i < 100000; i++) {
        str=str+"abc";
        }
       long endtime=System.currentTimeMillis();
       String strtime=endtime-starttime+"";
       System.out.println("string花费时间"+strtime+"毫秒");

       StringBuffer strBuf=new StringBuffer("abc");
       long starttime1=System.currentTimeMillis();    
       for (int i = 0; i < 100000; i++) {
        strBuf=strBuf.append("abc");
       }
       long endtime1=System.currentTimeMillis();
       String strtime1=endtime1-starttime1+"";
       System.out.println("stringBuffer花费时间"+strtime1+"毫秒");
       StringBuilder strbui=new StringBuilder("abc");
       long starttime2=System.currentTimeMillis();  
       for (int i = 0; i < 100000; i++) {
        strbui=strbui.append("abc");
       }
       long endtime2=System.currentTimeMillis();
       String strtime2=endtime2-starttime2+"";
       System.out.println("stringBuilder花费时间"+strtime2+"毫秒");
    }
    }

结果:string花费时间18141毫秒
stringBuffer花费时间5毫秒
stringBuilder花费时间4毫秒

多次试验得到速度StringBuilder>stringBuffer>string

  1. 解释主要是由于string是个常量,每次拼接实际上是生成一个新的string对象,而stringBuilder和stringBuffer是分配一片内存,不够再加,操作是的同一个对象,由于这个是单线程,所以stringBuilder比stringBuffer稍微快一点。

StringBuffer,StringBuilder默认是16个字节

 public StringBuffer() {
        super(16);
    }
     public StringBuilder() {
        super(16);
    }

3.线程安全


class Ticket implements  Runnable {
    private static int t=0;

     StringBuffer sb = new StringBuffer();  
     StringBuilder sc=new StringBuilder();
    boolean flag=true;  
     public void run() {    
        while (flag) {
            if (t<100) {
                  sb.append(t+"a ");
                  sc.append(t+"a ");
                  t++;            
                    }else {                                             
                        flag=false;                                                         
                        System.out.println("stringbuffer"+sb);
                        System.out.println("stringbuilder"+sc);
                    }           
        }

    }   
}
public class Thread7 {
     public static  void main(String[] args)
     {
        Ticket t=new Ticket();
        Thread t1=new Thread(t);   
        Thread t2=new Thread(t);
        Thread t3=new Thread(t);
        t1.start();
        t2.start();     
        t3.start();

     }
}

结果stringBuilder线程是不安全的

String,StringBuffer,StringBuilder性能比较,线程安全测试,源码解析。_第1张图片
源代码

synchronized关键字
 @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;
        super.append(str);
        return this;
  没有关键字
 @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }

你可能感兴趣的:(java,string,stringbuilder,stringbuffer,线程安全)