Admit nothing. Blame everyone. Be bitter.
IBM 286, SCJP, SCWCD, EIEIO
Impossible is I M Possible
Impossible is I M Possible
Originally posted by prashant bhogvan:
however .....anyone out there who can tell me this IMMUTABLE thing in a easy to understand way .... i dont get one thing ...when strings are immutable ...then y r they changed when u do concatination or use + operator on it
My theory of evolution is that Darwin was adopted. - Steven Wright
Originally posted by Si Brewster:
In addition, don't get confused by the advice to use StringBuffer for performance.
In practise the advice to use StringBuffer only holds when concatenated items are only resolvable at runtime, and not compile time (as per the example in the previous entry to this thread).
e.g.
String myDynamicString = ...(some dynamic value)...
...
String myConcatenatedString = "This is the String: " + myDynamicString;
the above would cause the creation of 2 String objects at the line creating myConcatenatedString. A possible exception to this is that the static String reference will reuse an existing String instance if the same static String has already been used.
Performing the above using a StringBuffer avoids the creation of the extra String.
But:
String myConcatenatedString = "This is the String: " + "here it is";
will not result in an extra String object since the compiler will resolve the above to a single String (possible because both Strings are static). Thus, there is no performance loss in splitting a static String across multiple lines for readability.
Also note that the StringBuffer causes it's own overhead and so for a small number of concatenations might make things slower than creating an extra String object anyway. It might not - you'd need to test to check this.
My theory of evolution is that Darwin was adopted. - Steven Wright
| With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |