# Replace Concatenation with Infix-Operation

# Description

This rule replaces the concat() method on Strings with the + operator.

For example, s = s.concat("bar") becomes s = s + "bar". This improves readability and performance for chained concatenations.

# Benefits

Applying this rule has slight performance benefits and arguably improves readability. The Java compiler will convert the + operation to use a StringBuilder. The more concatenations occur, the bigger the performance gain is.

# Tags

# Code Changes

# Base case


String s = "foo";
s = s.concat("bar");


String s = "foo";
s = s + "bar";

# Chained concatenation expression




input + "abc" + "cde" + "fgh" + "hij";

# Nested concatenation expressions




input + param + param;

# Bytecode JDK 1.8


public void original() {
    String value = "foo".concat("bar");
0 ldc #2 <foo>
2 ldc #3 <bar>
4 invokevirtual #4 <java/lang/String.concat>
7 astore_1
8 return


public void transformed() {
    String value = "foo" + "bar";
0 ldc #5 <foobar>
2 astore_1
3 return

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID StringConcatToPlus
First seen in jSparrow version 1.0.0
Minimum Java version 1.1
Remediation cost 5 min