Tuesday 17 January 2012

Expression evaluation can be compile time or runtime

Consider the expression:
String a = "a" + "b" + "c";
String b = System.getProperty("blah") + "b";

The first is evaluated at compile-time. The second is evaluated at run-time. So in case of strings,
never replace constant concatenations (of any type) with StringBuilder, StringBuffer or the like. Only use those where variables are invovled and generally only when you're appending a lot of operands or you're appending in a loop.

No comments:

Post a Comment