Java – the difference between string, string builders, character arrays and ArrayList

Strings are immutable String builders are not, so you can add characters at the end String is a character array. If I'm not wrong, why do we use character array and string separately? Do we really need to use character array?

Second, there are character arrays, and then there are ArrayLists Array list contains complete objects? Actually, I'm a little confused

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','l','e'};

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

Which should be used where?

Solution

This explains the best: between string and StringBuilder

Note that the goal (usually) is to reduce memory loss rather than total memory used to make life easier for the garbage collector

Will that take memory equal to using String like below?

No, it will cause more memory loss, not just the direct connection you refer to (until / unless the JVM optimizer finds that the explicit StringBuilder in the code is unnecessary and optimizes it, if possible.)

If the author of the code wants to use StringBuilder (there are arguments, but there are objections; see the comments at the end of this answer), it's best to do it correctly (here I assume that it doesn't actually revolve 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();

Notice that I listed some in the StringBuilder constructor_ appropriate_ Size, so it has enough capacity at the beginning to accommodate the complete content we want to add If one character is not specified, the default size used is 16 characters, which is usually too small for StringBuilder to reallocate to make itself larger (IIRC, in sun / Oracle JDK, it doubles itself [or more if it knows it needs more to meet a specific append] every time it runs out of room)

You may have heard that if compiled with the sun / Oracle compiler, string connections will use StringBuilder This is the fact that it will use a StringBuilder as the overall expression But it will use the default constructor, which means that in most cases it will have to be reassigned However, it is easier to read Note that this is not the case for a series of connections For example, this uses a StringBuilder:

return“prefix”variable1“middle”variable2“end”;

It roughly translates as:

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 it doesn't matter. Although the default constructor and subsequent reassignment are not ideal, it's possible enough - and the connection is more readable

But this applies only to a single expression Multiple stringbuilders are used for this:

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

Finally, it becomes such a thing:

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;

... it's ugly

However, it is important to remember that in rare cases, except for specific performance problems, it is irrelevant in all cases, and readability (enhanced maintainability) is the first choice

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>