码迷,mamicode.com
首页 > 其他好文 > 详细

String、StringBuffer、StringBuilder区别并验证

时间:2017-10-24 19:40:12      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:nan   ace   ble   i++   private   oid   验证   countdown   base   

? 版权声明:本文为博主原创文章,转载请注明出处

String、StringBuffer、StringBuilder的区别

1.String是一个常量,其对象一旦创建完毕就无法改变,当使用“+”拼接字符串时,会生成新的String对象,而不是向原有的String对象追加内容。

 StringBuffer和StringBuilder都是变量,追加内容时不会生成新的对象。

2.String初始化的时候是最快的,StringBuilder其次,StringBuffer最慢

 但是修改内容的时候StringBuilder是最快的,StringBuffer其次,String最慢

3.String是常量,因此线程安全。

 StringBuffer是线程安全的,StringBuilder是非线程安全的

验证

1.String是常量,改变值会生成新的对象;StringBuffer和StringBuilder是变量,改变值不会生成新的对象

技术分享
 1 /**
 2      * 内存比较
 3      */
 4     private static void memery() {
 5 
 6         String str1 = "abc";
 7         System.out.println("String first memery address is " + str1.hashCode());
 8         str1 += "efg";
 9         System.out.println("String second memery address is " + str1.hashCode());
10 
11         StringBuilder sb1 = new StringBuilder("abc");
12         System.out.println("StringBuilder first memery address is " + sb1.hashCode());
13         sb1.append("efg");
14         System.out.println("StringBuilder second memery address is " + sb1.hashCode());
15 
16         StringBuffer sb2 = new StringBuffer("abc");
17         System.out.println("StringBuffer first memery address is " + sb2.hashCode());
18         sb2.append("efg");
19         System.out.println("StringBuffer second memery address is " + sb2.hashCode());
20 
21     }
memery

结果:

技术分享

2.初始化速度:String>StringBuilder>StringBuffer

技术分享
 1 /**
 2      * 初始化性能比较
 3      */
 4     private static void initial() {
 5 
 6         long time1 = System.nanoTime();
 7         String str1 = "abc";
 8         long time2 = System.nanoTime();
 9         System.out.println("String time: " + (time2 - time1) + "ns");
10 
11         long time3 = System.nanoTime();
12         StringBuffer sb1 = new StringBuffer("abc");
13         long time4 = System.nanoTime();
14         System.out.println("StringBuffer time: " + (time4 - time3) + "ns");
15 
16         long time5 = System.nanoTime();
17         StringBuilder sb2 = new StringBuilder("abc");
18         long time6 = System.nanoTime();
19         System.out.println("StringBuilder time: " + (time6 - time5) + "ns");
20 
21     }
initial

结果:

技术分享

3.执行速度(即改变值的时候):StringBuilder>StringBuffer>String

技术分享
 1 /**
 2      * 性能比较
 3      */
 4     private static void performance() {
 5 
 6         long time1 = System.nanoTime();
 7         String str1 = "";
 8         for (int i = 0; i < 10; i++) {
 9             str1 += i;
10         }
11         long time2 = System.nanoTime();
12         System.out.println("the time required for String to execute 1000 times: " + (time2 - time1) + "ns");
13 
14         long time3 = System.nanoTime();
15         StringBuffer sb1 = new StringBuffer();
16         for (int i = 0; i < 10; i++) {
17             sb1.append(i);
18         }
19         long time4 = System.nanoTime();
20         System.out.println("the time required for StringBuffer to execute 1000 times: " + (time4 - time3) + "ns");
21 
22         long time5 = System.nanoTime();
23         StringBuilder sb2 = new StringBuilder();
24         for (int i = 0; i < 10; i++) {
25             sb2.append(i);
26         }
27         long time6 = System.nanoTime();
28         System.out.println("the time required for StringBuilder to execute 1000 times: " + (time6 - time5) + "ns");
29 
30     }
performance

结果:

 技术分享

4.特殊例子:当执行String str = "a" + "b" + "c"时,String的速度是最快的,因为等价于String = "abc";上面讲过,初始化的时候String是最快的

技术分享
 1 /**
 2      * 特殊例子
 3      */
 4     private static void special() {
 5 
 6         long time1 = System.nanoTime();
 7         String str1 = "abc" + "def" + "ghi" + "jk";
 8         long time2 = System.nanoTime();
 9         System.out.println("String time: " + (time2 - time1) + "ns");
10 
11         long time3 = System.nanoTime();
12         StringBuffer sb1 = new StringBuffer("abc").append("def").append("ghi").append("jk");
13         long time4 = System.nanoTime();
14         System.out.println("StringBuffer time: " + (time4 - time3) + "ns");
15 
16         long time5 = System.nanoTime();
17         StringBuilder sb2 = new StringBuilder("abc").append("def").append("ghi").append("jk");
18         long time6 = System.nanoTime();
19         System.out.println("StringBuilder time: " + (time6 - time5) + "ns");
20 
21     }
special

结果:

技术分享

5.StringBuffer是线程安全的,StringBuilder是非线程安全的

技术分享
 1 /**
 2      * 线程安全性验证
 3      */
 4     public static void thread() {
 5 
 6         final StringBuffer sb1 = new StringBuffer();
 7         final StringBuilder sb2 = new StringBuilder();
 8         final CountDownLatch latch1 = new CountDownLatch(1000);
 9         final CountDownLatch latch2 = new CountDownLatch(1000);
10 
11         for (int i = 0; i < 1000; i++) {
12             new Thread(new Runnable() {
13                 public void run() {
14                     try {
15                         sb1.append("1");
16                     } finally {
17                         latch1.countDown();
18                     }
19 
20                 }
21             }).start();
22         }
23         for (int i = 0; i < 1000; i++) {
24             new Thread(new Runnable() {
25                 public void run() {
26                     try {
27                         sb2.append("2");
28                     } finally {
29                         latch2.countDown();
30                     }
31                 }
32             }).start();
33         }
34 
35         try {
36             latch1.await();
37             System.out.println(sb1.length());
38             latch2.await();
39             System.out.println(sb2.length());
40         } catch (Exception e) {
41             e.printStackTrace();
42         }
43 
44     }
thread

结果:需多执行几次,StringBuffer长度全部为1000,StringBuilder大部分都是小于1000的

技术分享

6.完整代码

技术分享
  1 package org.java.base;
  2 
  3 import java.util.concurrent.CountDownLatch;
  4 
  5 /**
  6  * String、StringBuffer、StringBuilder比较
  7  */
  8 public class StringAndStringBufferAndStringBuilder {
  9 
 10     public static void main(String[] args) {
 11 
 12         //memery();
 13         //initial();
 14         //performance();
 15         //special();
 16         thread();
 17 
 18     }
 19 
 20     /**
 21      * 内存比较
 22      */
 23     private static void memery() {
 24 
 25         String str1 = "abc";
 26         System.out.println("String first memery address is " + str1.hashCode());
 27         str1 += "efg";
 28         System.out.println("String second memery address is " + str1.hashCode());
 29 
 30         StringBuilder sb1 = new StringBuilder("abc");
 31         System.out.println("StringBuilder first memery address is " + sb1.hashCode());
 32         sb1.append("efg");
 33         System.out.println("StringBuilder second memery address is " + sb1.hashCode());
 34 
 35         StringBuffer sb2 = new StringBuffer("abc");
 36         System.out.println("StringBuffer first memery address is " + sb2.hashCode());
 37         sb2.append("efg");
 38         System.out.println("StringBuffer second memery address is " + sb2.hashCode());
 39 
 40     }
 41 
 42     /**
 43      * 初始化性能比较
 44      */
 45     private static void initial() {
 46 
 47         long time1 = System.nanoTime();
 48         String str1 = "abc";
 49         long time2 = System.nanoTime();
 50         System.out.println("String time: " + (time2 - time1) + "ns");
 51 
 52         long time3 = System.nanoTime();
 53         StringBuffer sb1 = new StringBuffer("abc");
 54         long time4 = System.nanoTime();
 55         System.out.println("StringBuffer time: " + (time4 - time3) + "ns");
 56 
 57         long time5 = System.nanoTime();
 58         StringBuilder sb2 = new StringBuilder("abc");
 59         long time6 = System.nanoTime();
 60         System.out.println("StringBuilder time: " + (time6 - time5) + "ns");
 61 
 62     }
 63 
 64     /**
 65      * 性能比较
 66      */
 67     private static void performance() {
 68 
 69         long time1 = System.nanoTime();
 70         String str1 = "";
 71         for (int i = 0; i < 10; i++) {
 72             str1 += i;
 73         }
 74         long time2 = System.nanoTime();
 75         System.out.println("the time required for String to execute 1000 times: " + (time2 - time1) + "ns");
 76 
 77         long time3 = System.nanoTime();
 78         StringBuffer sb1 = new StringBuffer();
 79         for (int i = 0; i < 10; i++) {
 80             sb1.append(i);
 81         }
 82         long time4 = System.nanoTime();
 83         System.out.println("the time required for StringBuffer to execute 1000 times: " + (time4 - time3) + "ns");
 84 
 85         long time5 = System.nanoTime();
 86         StringBuilder sb2 = new StringBuilder();
 87         for (int i = 0; i < 10; i++) {
 88             sb2.append(i);
 89         }
 90         long time6 = System.nanoTime();
 91         System.out.println("the time required for StringBuilder to execute 1000 times: " + (time6 - time5) + "ns");
 92 
 93     }
 94 
 95     /**
 96      * 特殊例子
 97      */
 98     private static void special() {
 99 
100         long time1 = System.nanoTime();
101         String str1 = "abc" + "def" + "ghi" + "jk";
102         long time2 = System.nanoTime();
103         System.out.println("String time: " + (time2 - time1) + "ns");
104 
105         long time3 = System.nanoTime();
106         StringBuffer sb1 = new StringBuffer("abc").append("def").append("ghi").append("jk");
107         long time4 = System.nanoTime();
108         System.out.println("StringBuffer time: " + (time4 - time3) + "ns");
109 
110         long time5 = System.nanoTime();
111         StringBuilder sb2 = new StringBuilder("abc").append("def").append("ghi").append("jk");
112         long time6 = System.nanoTime();
113         System.out.println("StringBuilder time: " + (time6 - time5) + "ns");
114 
115     }
116 
117     /**
118      * 线程安全性验证
119      */
120     public static void thread() {
121 
122         final StringBuffer sb1 = new StringBuffer();
123         final StringBuilder sb2 = new StringBuilder();
124         final CountDownLatch latch1 = new CountDownLatch(1000);
125         final CountDownLatch latch2 = new CountDownLatch(1000);
126 
127         for (int i = 0; i < 1000; i++) {
128             new Thread(new Runnable() {
129                 public void run() {
130                     try {
131                         sb1.append("1");
132                     } finally {
133                         latch1.countDown();
134                     }
135 
136                 }
137             }).start();
138         }
139         for (int i = 0; i < 1000; i++) {
140             new Thread(new Runnable() {
141                 public void run() {
142                     try {
143                         sb2.append("2");
144                     } finally {
145                         latch2.countDown();
146                     }
147                 }
148             }).start();
149         }
150 
151         try {
152             latch1.await();
153             System.out.println(sb1.length());
154             latch2.await();
155             System.out.println(sb2.length());
156         } catch (Exception e) {
157             e.printStackTrace();
158         }
159 
160     }
161 
162 }
StringAndStringBufferAndStringBuilder

 

String、StringBuffer、StringBuilder区别并验证

标签:nan   ace   ble   i++   private   oid   验证   countdown   base   

原文地址:http://www.cnblogs.com/jinjiyese153/p/7725022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!