4

Strings are immutable. Stringbuilders are not, so you can append characters at the end. Strings are character arrays if i am not wrong, than why do we use character arrays separately and Strings separately, Do we really need to use character arrays?

Secondly, there are character arrays and then there are Arraylists. Array lists holds complete objects? I am a bit confused actually.

String cat = "c" + "a" + "t";
cat = cat + cat;

StringBuilder sb = new StringBuilder();
sb.append(city);
sb.append(", ");
sb.append(state);
sb.toString();

Char apple[5]={'a','p','p','l','e'};

Arraylist<MyCar>obj = new Arraylist<MyCar>();

Which should be used where?

Pod
  • 3,938
  • 2
  • 37
  • 45

4 Answers4

8

This Explain the best: between string and stringBuilder Ref:Correct way to use StringBuilder

Note that the aim (usually) is to reduce memory churn rather than total memory used, to make life a bit easier on the garbage collector.

Will that take memory equal to using String like below?

No, it'll cause more memory churn than just the straight concat you quoted. (Until/unless the JVM optimizer sees that the explicit StringBuilder in the code is unnecessary and optimizes it out, if it can.)

If the author of that code wants to use StringBuilder (there are arguments for, but also against; see note at the end of this answer), better to do it properly (here I'm assuming there aren't actually quotes around id2 and table):

StringBuilder sb = new StringBuilder(some_appropriate_size);
sb.append("select id1, ");
sb.append(id2);
sb.append(" from ");
sb.append(table);
return sb.toString();

Note that I've listed some_appropriate_size in the StringBuilder constructor, so that it starts out with enough capacity for the full content we're going to append. The default size used if you don't specify one is 16 characters, which is usually too small and results in the StringBuilder having to do reallocations to make itself bigger (IIRC, in the Sun/Oracle JDK, it doubles itself [or more, if it knows it needs more to satisfy a specific append] each time it runs out of room).

You may have heard that string concatenation will use a StringBuilder under the covers if compiled with the Sun/Oracle compiler. This is true, it will use one StringBuilder for the overall expression. But it will use the default constructor, which means in the majority of cases, it will have to do a reallocation. It's easier to read, though. Note that this is not true of a series of concatenations. So for instance, this uses one StringBuilder:

return "prefix " + variable1 + " middle " + variable2 + " end";

It roughly translates to:

StringBuilder tmp = new StringBuilder(); // Using default 16 character size
tmp.append("prefix ");
tmp.append(variable1);
tmp.append(" middle ");
tmp.append(variable2);
tmp.append(" end");
return tmp.toString();

So that's okay, although the default constructor and subsequent reallocation(s) isn't ideal, the odds are it's good enough — and the concatenation is a lot more readable.

But that's only for a single expression. Multiple StringBuilders are used for this:

String s;
s = "prefix ";
s += variable1;
s += " middle ";
s += variable2;
s += " end";
return s;

That ends up becoming something like this:

String s;
StringBuilder tmp;
s = "prefix ";
tmp = new StringBuilder();
tmp.append(s);
tmp.append(variable1);
s = tmp.toString();
tmp = new StringBuilder();
tmp.append(s);
tmp.append(" middle ");
s = tmp.toString();
tmp = new StringBuilder();
tmp.append(s);
tmp.append(variable2);
s = tmp.toString();
tmp = new StringBuilder();
tmp.append(s);
tmp.append(" end");
s = tmp.toString();
return s;

...which is pretty ugly.

It's important to remember, though, that in all but a very few cases it doesn't matter and going with readability (which enhances maintainability) is preferred barring a specific performance issue.

Community
  • 1
  • 1
Ami Patel
  • 296
  • 1
  • 13
2

Normally String is used for normal string based requirement, and when a String can suffice it.

String Builder is used whenever you want to manipulate and play with the string.

Character Array is used when you want to easily iterate over each and every character

ArrayList is a collection. Use it for holding object of a particular type.

Dushyant Gupta
  • 507
  • 1
  • 5
  • 24
  • I like your answer. how can i upvote it? the site does not allowing me to. it says i need 15 reputation – user3510103 Apr 08 '14 at 09:03
  • 1
    @user3510103 It means that you will be able to upvote when you get 15 reputation (now you got 12). This is simple security mechanism preventing bots from voting. Here you can find simple explanation on how to get (or lose) reputation http://stackoverflow.com/help/whats-reputation – Pshemo Apr 08 '14 at 09:12
1

String is immutable object that includes underlying char array.

In your line 2 you discard your String that you created in line 1 and create a new String.
String builder avoids creating new String objects for every separate substring.

Both arrays and Arraylist can contain objects, the main difference is that Arraylist can grow, arrays can not. The second difference is that Arraylist is really a List...

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
1

A String uses a char[]. A String is not a char[], in the same way that an ArrayList<String> is not a String[].

ArrayList type is a dynamic data structure. This means that it can grow depending on need. Array is static, meaning it's dimensions do not change over its lifetime.

christopher
  • 26,815
  • 5
  • 55
  • 89