I'm used to use String.format() when constructing a SQL statement, and a friend I knew likes to concatenate
saying :"it's more readable to me".
So
to concatenate use StringBuffer (Synchronized) or StringBuilder (not Synchronized) As both uses an internal array "so that new String Objects are not created".
saying :"it's more readable to me".
So
- String Objects are immutable, meaning that once they are created, they can't be altered.
- Concatenating 2 strings doesn't modify either Strings instead, it creates a new String "old ones are added to the string pool".
- String literals always have a reference to them in String Literal Pool, therefore not eligible for garbage collection.
to concatenate use StringBuffer (Synchronized) or StringBuilder (not Synchronized) As both uses an internal array "so that new String Objects are not created".
String literal Pool :
String are stored in pool and before creating a new string literals, compiler checks if such string already defined "used to optimize and save space".
String literals : a sequence of characters between quotation marks.
Thanks for he article.
ReplyDeleteBy the way, the java compiler replaces string concats with StringBuilder object in modern versions of JDK.
For concate was referring to
ReplyDeleteIf x="hello"; x+="world" this gives two 'hello' & 'hello world'
If concatenating in the same line x="hello "+"world"; this do string builder