• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is better?

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Bhavik patel
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sunny...

well..This will help you...

Appending strings
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic