Forums Register Login

Which is better?

+Pie Number of slices to send: Send
I have two option to create a String which looks somethig like this, Can any one help me to know which is better ;



I know in the first case,
Number of immutable Object : 7
Number of mutable Object : 0

In second Case :
Number of Immutable Object : 5
Number of mutable Object : 1

So Over all i saved 2 immutable Object. If I have to create similar String 20 times in a program, I will save 20 Objects.
My query is that which one i should prefer to use while coding?

first one is easy to understand and use. Second one is more spacious and not easy to understand ?
Which one is generally prefer and why ?
+Pie Number of slices to send: Send
If we take a look at your specific example, then the first is better, because the compiler is smart enough to concatenate the strings "javaRanch" + " is" + " good" + " site" at compile time. So, writing:

String str = "javaRanch" + " is" + " good" + " site";

is, at runtime, exactly the same as writing:

String str = "javaRanch is good site";

Note however, that if you use the + operator to concatenate strings that are not compile-time constants, the compiler will convert this automatically to code that uses StringBuffer (or StringBuilder). That can be less efficient than using StringBuilder yourself. Have a look at the following example:

The compiler will automatically convert this to something like this:

This is quite inefficient code: in every iteration of the loop, a new StringBuilder object is created, the contents of 'result' is copied into it, then a value is concatenated to it, and then the content of the StringBuilder is copied into a new String object again which is assigned to 'result'. So, there are a lot of temporary objects and a lot of copying of data going on.

Suppose you wrote it yourself like this:

That is a lot more efficient - there is only one StringBuilder object, and no unnecessary copying.
[ May 15, 2008: Message edited by: Jesper Young ]
+Pie Number of slices to send: Send
Jasper, I have one more query :
what if i have following :

String str = "javaRanch" + " is" + " a" + status + " site";

where status is a variable whose value can change at run time ?
At this situation what is the best way ?
+Pie Number of slices to send: Send
Hi sunny...

well..This will help you...

Appending strings
+Pie Number of slices to send: Send
 

Originally posted by Sunny Jain:
Jasper, I have one more query :


Try thinking about it yourself.

Note the following: If you have compile-time constants (values of which the value is fixed at compile time), the compiler will do the work beforehand. If not, then the compiler will convert the code to something with StringBuilder.

Look at your code, and think about what the compiler would do with it.

Note that problems with inefficiency especially happen when you use + on strings inside a loop - it's not such a problem when it's not in a loop.
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1566 times.
Similar Threads
why dont we prefer to use StringBuffer
How to make class immutable ?
A setter that can only be called by framework
Number of Object Created?
String vs StringBuilder
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:27:34.