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 .

4
  • "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" Commented Apr 27, 2015 at 8: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 Commented Apr 27, 2015 at 8:10
  • i have a large application in which i am using strings to assign and change values. should i use string or sb. Commented Apr 27, 2015 at 9:58
  • btw i understood ur point that tostring is offset itself better avoid it .thanks Commented Apr 27, 2015 at 10:02

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

It should also be noted that the compiler can replace uses of String with StringBuilder (in relationship to concatenration)
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.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.