[String] concat() vs. +

Screen Shot 2016-08-07 at 4.44.23 PM

concat() and + both add up strings. It looks same, but + do one more step. As seen below, + will create string builder and append the strings inside, and then it will give the string. So, concat() is less expensive.

/*      Using + sign      */
String myStr = "str";
myStr += "end";

// Same as above
String myStr = "str";
myStr = new StringBuilder().append(myStr).append("end").toString();


/*      Using concat()     */
String myStr = "str";
myStr = myStr.concat("end");


Leave A Comment

Your email address will not be published. Required fields are marked *