1

My program want to store Strings and no need to perform any operations on those except storing information as it is.

Then which of the following way is preferrable?

String s = new String("sagar");

or

StringBuilder sb = new StringBuilder("sagar");
akash
  • 22,664
  • 11
  • 59
  • 87
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
  • [Check out this link.](http://www.techtamasha.com/difference-between-string-and-stringbufferstringbuilder-in-java/28) – Wold Nov 01 '14 at 06:45
  • 2
    Unless you're storing some unworldly amount of text within your program, you really shouldn't focus on an optimization like this unless you've proven that this is a bottleneck. – Makoto Nov 01 '14 at 06:46
  • possible duplicate of [String, StringBuffer, and StringBuilder](http://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder) – Joe Nov 01 '14 at 06:54

5 Answers5

3

A String is a reference to an instance in the String intern pool. Since you aren't performing any string concatenation or manipulation here, I would use

 String s = "sagar";

I would not use

 String s = new String("sagar");

Because that ignores the intern pool. As for the StringBuilder I would prefer that when manipulating the value because (in contrast to String) it is mutable.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

For your question "which of the following way is preferrable?"

it really depends on your usage.

If you need to really perform manipulations on your string then StringBuilder is more useful then a normal String.

Moreover StringBuffer is also thread safe so it can be used effectively in a multithreaded environment.

Strings are immutable in nature.

Hope this helps!

Good luck.

Vihar
  • 3,626
  • 2
  • 24
  • 47
1

You should use String s = "sagar"; because it enables string literal pooling.

What is string literal pooling? - Like any other object creation,String creation/allocation is a memory and time consuming operation. Hence whenever a string is created it will refer to the string pool for the similar object, If such a string exists then it will refer the same.

This feature is possible as strings are immutable in java. Eventually string literal pooling will enhance the performance.

String Builder are intended for manipulations with Strings like concat().

Manoj Namodurai
  • 529
  • 2
  • 7
1

it is based on the using.

Actually at the time of concatenation String builder is effective than Strings.

In String Concatenation you can use String builder.

String builders are not thread safe so if you working on single thread it is effective but its not for multi-threading and Strings are immutable

Sivaram
  • 21
  • 3
  • @fastsnail Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used – Sagar Pudi Nov 04 '14 at 08:15
  • @SagarPudi if you knew the answer you have to answer rather than change someones answer completely – Madhawa Priyashantha Nov 04 '14 at 08:23
0

For storage - String

For manipulation - StringBuilder

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45