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
  • 97,681
  • 90
  • 411
  • 885
Amos94
  • 159
  • 1
  • 2
  • 11

8 Answers8

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,783
  • 15
  • 70
  • 117
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

Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
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

jaimin
  • 563
  • 8
  • 25
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,783
  • 15
  • 70
  • 117
lucian.marcuta
  • 1,250
  • 1
  • 16
  • 29
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,129
  • 1
  • 17
  • 22
cozla
  • 137
  • 1
  • 11
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,783
  • 15
  • 70
  • 117
Shravan
  • 101
  • 1
  • 2
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"
DTHENG
  • 188
  • 1
  • 7
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.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59