When I declare a String
looks like:
String a= "Hello, World!"
What I would like to ask is: String
makes from a an array?
Furthermore I understand that StringBuffer
is special for text editing. If I use String
the text cannot be changed while executing the program, that means that if I want to change or work with a text I should use StringBuffer
.
Is that right guys?
8 Answers 8
Storage Area
1.1String
- Constant String Pool
1.2StringBuffer
- Heap
1.3StringBuilder
- HeapModifiable
2.1String
- No (immutable)
2.2StringBuffer
- Yes( mutable )
2.3StringBuilder
- Yes( mutable )Thread Safe
3.1String
- Yes
3.2StringBuffer
- Yes
3.3StringBuilder
- NoPerformance
4.1String
- Fast
4.2StringBuffer
- Very slow
4.3StringBuilder
- Fast
Comments
Strings is immutable in java while the StringBuffer and StringBuilder classes are used when there is a necessity to make a lot of modifications to Strings of characters.
Unlike Strings objects of type StringBuffer and Stringbuilder can be modified over and over again with out leaving behind a lot of new unused objects.
The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilders methods are not thread safe(not Synchronised).
It is recommended to use StringBuilder whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects. example
public class Test{
public static void main(String args[]){
StringBuffer sBuffer = new StringBuffer(" test");
sBuffer.append(" String Buffer");
System.ou.println(sBuffer);
}
}
you can also use delete(int start, int end), public insert(int offset, int i),replace(int start, int end, String str) functions with string builder and buffer class
for more detail see this link
Comments
If multiusers are working at a String then better use StringBuffer
(Thread-safe)
If only one user will be editing that String better use StringBuilder
(Faster, Better Performance)
on the other side String
is immutable which means if you are NOT planning to edit your String , you'd better go for Simple String
object as it would be faster via caching
Comments
You can use both to work with text but the main difference is that 'StringBuffer' is faster than 'String' when performing concatenations.
Comments
You can always change a String in java like:
String value = "hello";
value = value + " world";
System.out.println(value); // "hello world"
But this is computationally intensive, you should use StringBuilder instead:
StringBuilder value = new StringBuilder("hello");
value.append(" world");
System.out.println(value.toString()); // "hello world"
Comments
hey you can change the value of a string
see example
String s="raju";
s=s+"ravi";
if we print s
it will print
raju ravi.
but StringBuffer
is for thread safe when you are using String
in multithreaded environment recommended to use StringBuffer
.
If you want not immutable and not multithreaded environment and better performance should recommended to use StringBuilder
Comments
If you have
String a="Hello";
and you make a=a+"World";
, then first string is "deleted", and you have a new string object(the object that a refer is changed). On the other hand, if you have
StringBuffer a=new StringBuffer("Hello");
and you write a.append("world");
, a remains unchanged(same reference, only the state of the object has been changed, not the reference). Sorry for my english..
public class Puzzle {
public static void main(String args[]) {
String a = new String("Hello");
String b = a;
System.out.println(a == b); //it s true
a = a + "world";
System.out.println(a == b); //it s false
StringBuffer c = new StringBuffer("Hello");
StringBuffer d = c;
System.out.println(c == d); //it s true
c.append("world");
System.out.println(c == d); //it s true
}
}
Comments
yes. If you want to store a string that is not going to change, you can use String datatype. If you want a datatype which supports value modification, StringBuilder or StringBuffer are better than String.
StringBuilder
is better thanStringBuffer
String
and other uses see ntu.edu.sg/home/ehchua/programming/java/j3d_string.html