This post will discuss how to append a character to the end of a string in Java.

1. Using String concat (+)

A simple solution to append a character to the end of a string is using the string concatenation operator (+). This creates a new instance of the string, since strings in Java are immutable and cannot be modified.

Download  Run Code

 
Alternatively, you can use the += operator, as shown below:

Download  Run Code

2. Using StringBuilder

Another plausible way of appending a character to the end of a string is using StringBuilder. The idea is to convert the string to a StringBuilder and call its append() method to append a character. Finally, convert the StringBuilder back to a string.

Note, you should prefer StringBuilder over StringBuffer since it’s not synchronized and faster. Here’s how the code would look like:

Download  Run Code

3. Using String.format() method

Finally, you can use the String.format() method that returns a formatted string from the specified format specifiers. This is demonstrated below:

Download  Run Code

That’s all about appending a character to the end of a string in Java.