String, StringBuffer and StringBuilder in Java | Convert String to StringBuffer or StringBuilder and vice versa
What is String, StringBuilder and StrinfBuffer in java?
In this article we talk about String, StringBuffer and StringBuilder in java.
String :-
String is class in java that represents sequence of characters, it can be word, number, special characters. String represents in Double Quotes (" ").
String is immutable in java.
What is immutable?
In simple word immutable means can not changable. it con not be replacable.
So String is immutable in java. we can not alter String object.
So there is one question is pop up in your mind. We can still concatenate or changed String object in java so how it is immutable in Java?
Yes, we can concatenate or changed String object but every time we perform these kind of operations it creates new Object of that. See below image you can easily understand.
Example 1 :- String demo with immutability
public static void main(String[] args) {
String str = "Programming Blog";
System.out.println(str.hashCode());
str = "Uniquethrowdown " + str;
System.out.println(str.hashCode());
}
}
Output :-
1799821001
-965964782
In above example, first we create String str and assign value. after we concatenate str. So you can see before and after changing String it changes hashCode() value means it creates new Object of that String class.
StringBuffer And StringBuilder :-
StringBuffer and StringBuilder are like string but it is mutable class in java means we can easily changed, modify or replace.
Use StringBuffer and StringBuilder when you have lots of modification in some string our code.
StringBuffer is thread-safe in java. Means StringBuffer object are safe use for multiple threads.
StringBuilder is not thread-safe in java. StringBuilder gives no guarantee of synchronization.
These is main difference between StringBuilder and StringBuffer in java.
Lets see example of StringBuilder and StringBuffer in java.
Example 2 :- StringBuilder and StringBuffer in java
public class StringDemo {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("Uniquethrowdown");
System.out.println(buffer.hashCode());
buffer = buffer.append(" Programming Blog");
System.out.println(buffer.hashCode());
StringBuilder builder = new StringBuilder("Java");
System.out.println(builder.hashCode());
builder = builder.reverse();
System.out.println(builder.hashCode());
}
}
Output :-
617901222
617901222
1159190947
1159190947
First we create StringBuffer object and then append string on that. but in output you can see hashCode is same before and after append operation. means when we change on StringBuffer class then it changes on same object it does not create another object like String class.
We can convert String to StringBuffer or StringBuilder in java. we can also convert StringBuilder or StringBuffer to String.
Example 3 :- Convert String to StringBuilder and StringBuffer in Java
public class StringDemo {
public static void main(String[] args) {
String str = "Programming Blog";
// Convert string to StringBuffer
StringBuffer buffer = new StringBuffer(str);
// Convert string to StringBuilder
StringBuilder builder = new StringBuilder(str);
System.out.println(str);
System.out.println(buffer);
System.out.println(builder);
}
}
Example 4 :- Convert StringBuffer or StringBuilder to String
public class StringDemo {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("Uniquethrowdown");
StringBuilder builder = new StringBuilder("Programming Blog");
String bufferToStr = buffer.toString();
System.out.println(bufferToStr);
System.out.println(bufferToStr instanceof String);
String builderToStr = builder.toString();
System.out.println(builderToStr);
System.out.println(builderToStr instanceof String);
}
}
Output :-
Uniquethrowdown
true
Programming Blog
true
We use instanceof of in above example. instanceof is used to check given object is from which class.
Refrences and images from :-
- https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
- https://dzone.com/articles/string-concatenation-performacne-improvement-in-ja
- https://www.javatpoint.com/difference-between-stringbuffer-and-stringbuilder
Other articles you may like.
- Double Brace Initialization in Java
- BiFunction in Java
- Lambda Expression in Java
- Functional Interface in Java
- Abstraction in Java
- Static in Java (Static class, Method, Variable and Block)
- Final keyword in java
- Difference between Final, Finally and Finalize in Java
Comments
Post a Comment