sentence
stringlengths 1
1.38k
| label
stringclasses 3
values |
---|---|
You use URL_http://java.sun.com/javase/6/docs/api/java/lang/String.html [CODETERM1] when an immutable structure is appropriate; obtaining a new character sequence from a CODETERM2 may carry an unacceptable performance penalty, either in CPU time or memory (obtaining substrings is CPU efficient because the data is not copied, but this means a potentially much larger amount of data may remain allocated).
|
n
|
You use URL_http://java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html [CODETERM3] when you need to create a mutable character sequence, usually to concatenate several character sequences together.
|
o
|
You use URL_http://java.sun.com/javase/6/docs/api/java/lang/StringBuffer.html [CODETERM4] in the same circumstances you would use CODETERM5 , but when changes to the underlying string must be synchronized (because several threads are reading/modifyind the string buffer).
|
o
|
See an example URL_http://java.sun.com/docs/books/tutorial/java/data/buffers.html [here] .
|
o
|
+1, concise answer.
|
o
|
concise, but incomplete, it misses the fundamental reason to use StringBuilder/Buffer and that is to reduce or eliminate the re-allocation and array copy of the regular String concatenation behavior.
|
o
|
"You use String when you are dealing with immutable strings" - doesn't make sense.
|
o
|
Instances of String ARE immutable, so maybe the comment should read "Use String when the memory usage due to immutability doesn't matter".
|
n
|
The accepted answer covers it's bases pretty well.
|
p
|
@fooMonster I've edited the answer.
|
o
|
Basics:** CODETERM1 is an immutable class, it can't be changed.
|
o
|
URL_http://java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html [CODETERM2] is a mutable class that can be appended to, characters replaced or removed and ultimately converted to a CODETERM3 CODETERM4 is the original synchronized version of CODETERM5 You should prefer CODETERM6 in all cases where you have only a single thread accessing your object.
|
p
|
Details:** Also note that CODETERM7 aren't magic, they just use an Array as a backing object and that Array has to be re-allocated when ever it gets full.
|
o
|
Be sure and create your CODETERM8 objects large enough originally where they don't have to be constantly re-sized every time CODETERM9 gets called.
|
p
|
The re-sizing can get very degenerate.
|
n
|
It basically re-sizes the backing Array to 2 times its current size every time it needs to be expanded.
|
o
|
This can result in large amounts of RAM getting allocated and not used when CODETERM10 classes start to grow large.
|
o
|
In Java CODETERM11 uses a CODETERM12 behind the scenes.
|
o
|
So for simple cases there is no benefit of declaring your own.
|
p
|
But if you are building CODETERM13 objects that are large, say less than 4k, then declaring CODETERM14 is much more efficient than concatenation or using the URL_http:/ /java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html#StringBuilder%28% 29 [default] which is only 16 characters.
|
p
|
If your CODETERM15 is going to be less than 10k then initialize it with the constructor to 10k to be safe.
|
o
|
But if it is initialize to 10k then you write 1 character more than 10k, it will get re-allocated and copied to a 20k array.
|
o
|
So initializing high is better than to low.
|
n
|
In the auto re-size case, at the 17th character the backing Array gets re- allocated and copied to 32 characters, at the 33th character this happens again and you get to re-allocated and copy the Array into 64 characters.
|
o
|
You can see how this degenerates to
of re-allocations and copies which is what you really are trying to avoid using CODETERM16 in the first place.
|
n
|
This is from the JDK 6 Source code for AbstractStringBuilder CODESNIPPET_JAVA1 .
|
o
|
A best practice is to initialize the CODETERM17 a little bit larger than you think you are going to need if you don't know right off hand how big the CODETERM18 will be but you can guess.
|
n
|
One allocation of slightly more memory than you need is going to be better than lots of re-allocations and copies.
|
n
|
Also beware of initializing a CODETERM19 with a CODETERM20 as that will only allocated the size of the String + 16 characters, which in most cases will just start the degenerate re-allocation and copy cycle that you are trying to avoid.
|
n
|
The following is straight from the Java 6 source code.
|
o
|
CODESNIPPET_JAVA2 .
|
o
|
If you by chance do end up with an instance of CODETERM21 that you didn't create and can't control the constructor that is called, there is a way to avoid the degenerate re-allocate and copy behavior.
|
n
|
Call URL_http://java.sun.c om/javase/6/docs/api/java/lang/StringBuilder.html#ensureCapacity%28int%29 [CODETERM22] with the size you want to ensure your resulting CODETERM23 will fit into.
|
o
|
Alternatives:** Just as a note, if you are doing really
CODETERM24 building and manipulation, there is a much more performance oriented alternative called URL_http://ahmadsoft.org/ropes/ [Ropes] .
|
o
|
Another alternative, is to create a CODETERM25 implemenation by sub- classing CODETERM26 , and adding counters to track the number of characters on every CODETERM27 and other mutation operations of the list, then override CODETERM28 to create a CODETERM29 of the exact size you need and loop through the list and build the output, you can even make that CODETERM30 an instance variable and 'cache' the results of CODETERM31 and only have to re-generate it when something changes.
|
o
|
Also don't forget about CODETERM32 when building fixed formatted output, which can be optimized by the compiler as they make it better.
|
p
|
Does String x
"A" + "B"; really compile to be a StringBuilder?
|
o
|
Why wouldn't it just compile to String x
"AB";, it should only use a StringBuilder if the components aren't known at compile time.
|
o
|
it might optimize the String constants out, I can't recall from the last time decompiled the byte code, but I do know that if there are any variables in there it will use a StringBuilder implementation for sure.
|
o
|
You can download the JDK source code and find out yourself.
|
o
|
"A" + "B" is a contrived example for sure.
|
o
|
I was wondering about String.format().
|
o
|
I haven't ever really seen it being used in projects.
|
o
|
Usually it's the StringBuilder.
|
o
|
Well, usually it's actually "A" + "B" + "C" because people are lazy ;) I tended to always use a StringBuilder, even if it was only two strings being concatenated, because in future, perhaps more strings would be appended.
|
p
|
I never used String.format() mainly because I never remembered what JDK it was introduced - I see it's JDK1.5, I'd use it in favour of the other options.
|
o
|
This is a very good article URL_http://kaioa.com/node/59 [ URL_http://kaioa.com/node/59 ] .
|
p
|
better article URL_http://java.sun.com/developer/technicalArticles/Interviews/comm unity/kabutz_qa.html.
|
o
|
Hi fuzzy lollipop,I do not understand your intension.
|
o
|
If the article you have posted is better doesn't mean that my article is wrong.
|
n
|
You should give -1 only if it is giving faulty info.
|
o
|
@Sujee :Yours is better +1.
|
o
|
Do you mean, for concatenation?
|
o
|
Real world example: _You want to create a new string out of many others_.
|
o
|
For instance to send a message: String CODESNIPPET_JAVA1 .
|
o
|
StringBuilder CODESNIPPET_JAVA2 .
|
o
|
Or CODESNIPPET_JAVA3 .
|
o
|
StringBuffer ( the syntax is exactly as with StringBuilder, the effects differ ) About CODETERM1 vs. CODETERM2 The former is synchonized and later is not.
|
o
|
So, if you invoke it several times in a single thread ( which is 90% of the cases ), CODETERM3 will run
faster because it won't stop to see if it owns the thread lock.
|
p
|
So, it is recommendable to use CODETERM4 ( unless of course you have more than one thread accessing to it at the same time, which is rare ) CODETERM5 concatenation ( _using the + operator_ ) may be optimized by the compiler to use CODETERM6 underneath, so, it not longer something to worry about, in the elder days of Java, this was something that everyone says should be avoided at all cost, because every concatenation created a new String object.
|
p
|
Modern compilers don't do this anymore, but still it is a good practice to use CODETERM7 instead just in case you use an "old" compiler.
|
p
|
Just for who is curious, this is what the compiler does for this class: CODESNIPPET_JAVA4 .
|
o
|
javap -c StringConcatenation CODESNIPPET_JAVA5 .
|
o
|
Lines numbered 5 - 27 are for the String named "literal" Lines numbered 31-53 are for the String named "builder" Ther's no difference, exactly the
code is executed for both strings.
|
o
|
this is a really bad example.
|
n
|
Initializing the StringBuilder with " Dear " means the first .append() will cause a reallocation and copy.
|
p
|
Completely negating any efficiency that you are trying to gain over "normal" concatenation.
|
o
|
A better example would to be to create it with a initial size that will hold the entire contents of the final String.
|
o
|
It is NOT generally a good practice to use a StringBuilder to do String concatenation on the right-hand side of the assignment.
|
p
|
Any good implementation will use a StringBuilder behind the scenes as you say.
|
p
|
Furthermore, your example of "a" + "b" would be compiled into a single literal "ab" but if you used StringBuilder it would result in two needless calls to append().
|
o
|
@Mark I didn't mean to use "a"+"b" but to say what was a
concatenation* about I change it to be explicit.
|
o
|
What you don't say is, why is not a good practice to do it.
|
o
|
That's exactly what ( a modern ) compiler does.
|
o
|
@fuzzy, I agree, specially when you know what the size of the final string would be ( aprox ) .
|
o
|
I don't think it's particularly
practice, but I certainly wouldn't want any recommendation to do that in general.
|
o
|
It's clunky and hard to read, and overly verbose.
|
n
|
Plus, it encourages mistakes like yours where you are breaking up two literals that could otherwise be compiled as one.
|
n
|
Only if profiling told me it made a difference would I do it your way.
|
o
|
@Mark Got it.
|
o
|
I was thinking more in the
chunk of code like a template"* and not really in every regular string literal.
|
o
|
But yes, I agree, since they do the same thing nowadays, it doesn't make sense ( 10 yrs ago was a reason to reject a change in a code revision ) :).
|
n
|
Personally, I don't think there is any real world use for CODETERM1 .
|
o
|
When would I ever want to communicate between multiple threads by manipulating a character sequence?
|
o
|
That doesn't sound useful at all, but maybe I have yet to see the light :) .
|
o
|
Also, CODETERM1 is thread-safe, which CODETERM2 is not.
|
p
|
So in a real-time situation when different threads are accessing it, CODETERM3 could have an undeterministic result.
|
o
|
Note that if you are using Java 5 or newer, you should use CODETERM1 instead of CODETERM2 .
|
o
|
From the API documentation:
As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, CODETERM3 .
|
o
|
The CODETERM4 class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
|
p
|
In practice, you will almost never use this from multiple threads at the same time, so the synchronization that CODETERM5 does is almost always unnecessary overhead.
|
o
|
When you are using String, you are making a new String each time you are adding 2 of them.
|
o
|
When you are using StringBuilder, it uses a buffer to make less String addition and will therefore be more performant when you want to add a lot of string one after the other.
|
o
|
StringBuffer is the exact same thing as of StringBuilder except that all his method are synchronized.
|
o
|
The only use of that class is if you need to share the buffer with multiple thread (which rarely happends).
|
o
|
Read/convert an InputStream to a String.
|
o
|
If you have CODETERM1 object, how should you process that object and produce a CODETERM2 ?
|
o
|
Suppose I have an CODETERM3 that contains text data, and I want to convert this to a CODETERM4 (for example, so I can write the contents of the stream to a log file).
|
o
|
What is the easiest way to take the CODETERM5 and convert it to a CODETERM6 ?
|
o
|
CODESNIPPET_JAVA1 .
|
o
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.