String concatenation - Boolean hard-coded Vs Boolean Concatenation with String

I need a advice (both in java & .net) for the following piece of code.

 public void method(bool value)
 {
     String someString;

     //some code

     if (value)
     {
          //some code
          ...
         someString = "one" + value;              
     }
     else
     {
         //some code
         ...
         someString = "two" + value;
     }

 }

Which one is advisable and why? either code like above or code like

someString = "onetrue";
someString = "twofalse";

Answers


After compilation and optimization by JDK, method will look like:

public static String method(boolean value) {
    String someString;

    if (value) {
        StringBuilder sb = new StringBuilder();
        sb.append("one");
        sb.append(value);
        someString = sb.toString();
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("two");
        sb.append(value);
        someString = sb.toString();
    }
    return someString;
}

If this code is invoked very frequently, it could bring a performance impact, compared to the second version. In each case a new StringBuilder is constructed and three methods are invoked on it. And boolean should be converted to an object before calling append. While in the second version we just return constant. Everything depends on how often this code is called.


Need Your Help

Data not fully loaded from Core Data when being called by UICollectionView to display cell

ios swift cocoa-touch core-data uicollectionview

I want to display some data in UICollectionView read from Core Data in viewDidLoad. However, when the UICollectionView is presenting its cells, the data has not been fully loaded yet. Thus, the

Fetch different URLs and write to file

html ruby nokogiri

I am trying to fetch different URLs e.g. site.com/page=1, page2 and so on. All fetched data should be stored in an HTML file to read it with Nokogiri.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.