Java中String和StringBuffer/StringBuilder的区别
2017-03-16 11:35
281 查看
Well, the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.
By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.
So suppose you declare a String object:
Next, you want to append “Guest” to the same String. What do you do?
When you print the contents of myString the output will be “Hello Guest”. Although we made use of the same object(myString), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.
Now isn’t that a performance issue?
Yes, it definitely is.
Then how do you make your string operations efficient?
By using StringBuffer or StringBuilder.
How would that help?
Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.
Finally, whats the difference between StringBuffer and StringBuilder?
StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).
So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.
Incase you still have any doubts regarding String or StringBuilder then do leave a comment. I’ll be more than eager to help you out.
Note: StringBuilder was introduced in Java 1.5 (so if you happen to use versions 1.4 or below you’ll have to use StringBuffer)
Incase you do not know – Here’s how you use StringBuilder
Now get out of that bad habit of using String objects to perform operations on strings. With StringBuilder(introduced in J2Se 5.0), you can perform those very append and other such operations more efficiently.
Scene 1: typical coder…. using the same old String object to perform append operations.
I agree it’ll give you “Hello World”, but fella…. let’s be different from the rest and think about making the code more efficient.
Enter Scene 2.
well… thats it!! You’ll get your “Hello World” but in a more efficient way.
A simple Example to demonstrate that String object is Immutable
Below is a simple example that will make you believe that what I said about String object is indeed true!
The output of the above code will be:
That’s all people! The above piece of code proves that String is immutable and hence the results of operations like concat etc. should be stored into a new object.
By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.
So suppose you declare a String object:
String myString = “Hello”;
Next, you want to append “Guest” to the same String. What do you do?
myString = myString + ” Guest”;
When you print the contents of myString the output will be “Hello Guest”. Although we made use of the same object(myString), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.
Now isn’t that a performance issue?
Yes, it definitely is.
Then how do you make your string operations efficient?
By using StringBuffer or StringBuilder.
How would that help?
Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.
Finally, whats the difference between StringBuffer and StringBuilder?
StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).
So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.
Incase you still have any doubts regarding String or StringBuilder then do leave a comment. I’ll be more than eager to help you out.
Note: StringBuilder was introduced in Java 1.5 (so if you happen to use versions 1.4 or below you’ll have to use StringBuffer)
Incase you do not know – Here’s how you use StringBuilder
Now get out of that bad habit of using String objects to perform operations on strings. With StringBuilder(introduced in J2Se 5.0), you can perform those very append and other such operations more efficiently.
Scene 1: typical coder…. using the same old String object to perform append operations.
String s = “Hello”; s = s + ” World”; system.out.println(s);
I agree it’ll give you “Hello World”, but fella…. let’s be different from the rest and think about making the code more efficient.
Enter Scene 2.
StringBuilder sb = new StringBuilder(“Hello”); sb.append(” World”); system.out.println(sb);
well… thats it!! You’ll get your “Hello World” but in a more efficient way.
A simple Example to demonstrate that String object is Immutable
Below is a simple example that will make you believe that what I said about String object is indeed true!
String s = “Let’s test”; s.concat(” if the String object is IMMUTABLE”); System.out.println(s); s = s.concat(” if the String object is IMMUTABLE”); System.out.println(s);
The output of the above code will be:
Let’s test Let’s test if the String object is IMMUTABLE
That’s all people! The above piece of code proves that String is immutable and hence the results of operations like concat etc. should be stored into a new object.
相关文章推荐
- Java软件低级错误(二十 ):正确理解String/StringBuffer/StringBuilder的性能区别
- Java中String/StringBuffer/StringBuilder区别
- 浅谈java中String StringBuffer StringBuilder的区别
- Java String,StringBuilder和StringBuffer的区别 StringBuilder > StringBuffer> String
- JAVA基础之InterView(一)--String+StringBuffer+StringBuilder的区别
- Java String/StringBuilder/StringBuffer的区别
- StringBuilder StringBuffer String的区别(源码分析)-java
- JAVA String StringBuffer StringBuilder 区别
- java.lang.StringBuffer与java.lang.StringBuilder的作用?区别是什么?
- java 面试专题(二)String StringBuffer StringBuilder的区别
- java语言:String StringBuffer StringBuilder区别
- String StringBuffer StringBuilder 三者的区别
- Java基础知识-String StringBuffer StringBuilder比较
- 我的安卓学习之路--String StringBuffer StringBuilder的区别
- JAVA String StringBuffer Stringbuilder
- String StringBuffer StringBuilder 三者的区别
- String StringBuffer StringBuilder的区别
- String StringBuffer StringBuilder 三者的区别
- String StringBuffer StringBuilder区别
- 黑马程序员_Java学习日记_JAVA中API中对象String和StringBuffer/StringBuilder