1

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?

jww
103k105 gold badges452 silver badges966 bronze badges
asked Jul 31, 2014 at 12:28
6

8 Answers 8

1
  1. Storage Area
    1.1 String - Constant String Pool
    1.2 StringBuffer - Heap
    1.3 StringBuilder - Heap

  2. Modifiable
    2.1 String - No (immutable)
    2.2 StringBuffer - Yes( mutable )
    2.3 StringBuilder - Yes( mutable )

  3. Thread Safe
    3.1 String - Yes
    3.2 StringBuffer - Yes
    3.3 StringBuilder - No

  4. Performance
    4.1 String - Fast
    4.2 StringBuffer - Very slow
    4.3 StringBuilder - Fast

Mohammad Faisal
5,95915 gold badges74 silver badges124 bronze badges
answered Jul 31, 2014 at 13:12
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Jul 31, 2014 at 12:34

Comments

0

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

answered Jul 31, 2014 at 12:34

Comments

0

You can use both to work with text but the main difference is that 'StringBuffer' is faster than 'String' when performing concatenations.

Kobor42
5,1471 gold badge20 silver badges23 bronze badges
answered Jul 31, 2014 at 12:35

Comments

0

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"
answered Apr 23, 2015 at 23:23

Comments

0

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

Mohammad Faisal
5,95915 gold badges74 silver badges124 bronze badges
answered Jul 31, 2014 at 12:39

Comments

0

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
 }
}
Mohammad Faisal
5,95915 gold badges74 silver badges124 bronze badges
answered Jul 31, 2014 at 12:35

Comments

0

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.

answered Jun 30, 2015 at 11:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.