java - Why use append() instead of + -
possible duplicate:
why use stringbuffer in java instead of string concatenation operator
what advantage or aim of doing this
int a= 42 stringbuffer sb = new stringbuffer(40); string s = sb.append("a = ").append(a).append("!").tostring(); system.out.println(sb);
result > = 42!
instead of
int a= 42 string s = "a = " + + "!"; system.out.println(sb);
in scenario, i'm not sure there difference b/c of "+" on 1 line (which believe creates 1 string). in general, though, strings immutable objects , not manipulated rather created , discarded using stringbuffers.
so ultimately, have more efficient code if use stringbuffers (and stringbuilders). if google "string vs. stringbuffer vs. stringbuilder" can find many articles detailing statistics.
Comments
Post a Comment