Eclipse Tips and Tricks, Converting String Concatenation to StringBuilder

adevedo's picture
0
No votes yet

String Concatenation is the process of appending 2 String objects to each other



Example :


String x = "A"

String y = "B"

String z = x + y


At the end of the execution of the above 3 lines, the String z will contain the value of "AB"



StringBuilder


As per the Javadoc, a mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.



This article shows how to use eclipse shortcuts to convert the String concatination operation to be based on StringBuilder instead of the + operator







A snap of code uses the + operator to perform String concatination (z = x + y)





Put the cursor on the (+) character and press Ctrl + 1 and select the item "Use StringBuilder for String Concatenation", press enter





A new StringBuilder object will be created





Type a name for the new object, press enter





The final result will be as in the image

Comments

Very good tip, Thanks friend.

use String.format

Add new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.
By submitting this form, you accept the Mollom privacy policy.