java - String concatenation - Boolean hard-coded Vs Boolean Concatenation with String -
i need advice (both in java & .net) 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 1 advisable , why? either code above or code like
somestring = "onetrue"; somestring = "twofalse";
after compilation , optimization jdk, method
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 code invoked frequently, bring performance impact, compared second version. in each case new stringbuilder constructed , 3 methods invoked on it. , boolean should converted object before calling append
. while in second version return constant. depends on how code called.
Comments
Post a Comment