0

Yes i have read all material on internet regarding their difference.and that difference is totally based on concatenation performance of both.My question is that in the below code which technique is better.

public class StringBuilderDemo {

   public static void main(String[] args) {

      StringBuilder str = new StringBuilder("test");
      System.out.println(str.toString());

      str = new StringBuilder("Hi "); 
      System.out.println(str.toString());
  }
}

here is string demo

public class StringDemo {
    static String str="";
    public static void main(String[] args) {

       str = "test";
       System.out.println(str);

       str ="Hi"; 
       System.out.println(str);
   }
}

My assumptions are since strings are immutable so when we assign "Hi" to str "test " also remain in memory(two objects of string created "Hi" and "test" ).where as in case of string builder when we give value "Hi" "test" is removed.so we have one object in case of string builder. So i concluded that we should use string builder in these cases. Correct me if i am being childish here .

Semih Eker
  • 2,389
  • 1
  • 20
  • 29
hussi
  • 77
  • 2
  • 9
  • "new StringBuilder("test")" creates a String object ("test") which StringBuilder converts to a char array (from memory), so in thhis instance, I would say the creation of a StringBuilder is more expensive (at least two objects) over simply using "test" – MadProgrammer Apr 27 '15 at 08:10
  • when you do strBuild.toString and new Strig is created .. so if its usefull only for large applications ... small projects wont be saving much if u use toString a lot – Srinath Ganesh Apr 27 '15 at 08:10
  • i have a large application in which i am using strings to assign and change values. should i use string or sb. – hussi Apr 27 '15 at 09:58
  • btw i understood ur point that tostring is offset itself better avoid it .thanks – hussi Apr 27 '15 at 10:02

4 Answers4

1

In your case an ordinary String is better. You should use StringBuilder in large for loops where you are adding a lot of stuff to a string.

The thing is that a String is imutable and when you assign a variable to a string, java looks in what you can imagine a table of already created ones. If there is one with the same content, you get a reference to that String. However, whenever you are chaining the content of the String, a new object is created and hence a slower performance in large loops.

With the StringBuilder that is not the case, it is mutable, which means that you can modify it's objects and there will be no new objects created, instead it will just resize itself when it needs to.

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • It should also be noted that the compiler can replace uses of String with StringBuilder (in relationship to concatenration) – MadProgrammer Apr 27 '15 at 08:08
1

You are right, String is immutable. Means you cannot add things to its memory content directly, meaning you'll need additional memory to access it. However, your application here doesn't seems to be memory intensive, hence you can just use String directly.

Ivan Ling
  • 127
  • 2
  • 10
0

Yep, when you join more string or you create a string there is a String Builder hided behind it.

For simple string there is no difference in performance but you should use the String Builder if u need join (or add) more strings togheter.

Ivan
  • 978
  • 6
  • 13
0

This is very basic thing. you should use 'String' not 'StringBuilder' in your case.

Mubeen Ali
  • 2,150
  • 2
  • 18
  • 26