An issue is raised when a primitive literal is converted to a string by concatenating it with an empty string rather than writing the resulting string literal directly or using an explicit conversion method.
This includes compile-time constant contexts when the concatenation can be replaced with an equivalent string literal. Annotation arguments,
annotation element defaults, constant case labels, and constant field initializers are not exempt.
When the targeted empty-string/primitive-literal pattern is part of a larger compile-time constant concatenation, simplify and report the enclosing
constant concatenation. For example, report the whole initializer "a" + ("" + 1) and recommend "a1". This rule does not
target arbitrary constant concatenations such as "a" + "b" unless they contain the targeted pattern.
Converting literals to strings by concatenating them with empty strings obscures the intended string value.
When concatenation is evaluated at runtime, it may involve intermediate objects or additional operations to build the resulting string. The exact behavior depends on the language, compiler, and runtime. These costs do not apply when the concatenation is evaluated at compile time.
The readability concern remains in either case: a developer reading an empty string concatenated with a literal must infer the resulting string, rather than seeing it directly.
Writing the resulting string literal directly makes the value clear. When an explicit conversion is appropriate, most languages also provide methods for converting primitives to strings:
These methods make the conversion intent explicit, but do not necessarily preserve compile-time constant semantics.
In Java, concatenations that are constant expressions are evaluated at compile time. For example, "" + 42 and "a" + ("" +
1) do not incur runtime string-building overhead. The reason to simplify these expressions is readability, not runtime performance.
This pattern reduces code readability and maintainability by obscuring the intended string value. Runtime concatenation may also have a performance cost, but concatenations evaluated at compile time do not incur runtime string-building overhead.
For compile-time constant concatenations, replace the enclosing constant concatenation with a string literal containing its evaluated value. Preserve the exact string value, including escaping where necessary.
Use this replacement in annotation arguments and annotation element defaults (including array elements), constant case labels in
switch statements or expressions, and constant field initializers. This also applies to implicitly static final fields in
interfaces and annotation types.
Method calls such as Integer.toString(1) and String.valueOf(1) are not constant expressions. They are unsuitable
replacements in annotation values, annotation element defaults, and constant case labels because the code would no longer compile. In a
constant field initializer, they would turn the field into a non-constant, potentially breaking uses in constant contexts and changing constant
inlining behavior. An equivalent string literal preserves these semantics.
Outside contexts where compile-time constant semantics must be preserved, an explicit conversion is also an option. For primitive types, use the
wrapper class’s static toString() method (e.g., Integer.toString(), Long.toString(),
Boolean.toString()) or String.valueOf().
@interface Label {
String value() default "" + 1; // Noncompliant
String[] aliases() default {"" + 2}; // Noncompliant
}
@Label(value = "" + 3, aliases = {"" + 4}) // Noncompliant
class Example {
static final String SIMPLE = "" + 42; // Noncompliant
static final String NESTED = "a" + ("" + 1); // Noncompliant: replace the whole initializer with "a1"
boolean matches(String value) {
switch (value) {
case "" + 5: // Noncompliant
return true;
default:
return false;
}
}
}
@interface Label {
String value() default "1";
String[] aliases() default {"2"};
}
@Label(value = "3", aliases = {"4"})
class Example {
static final String SIMPLE = "42";
static final String NESTED = "a1";
boolean matches(String value) {
switch (value) {
case "5":
return true;
default:
return false;
}
}
}
When constant semantics do not need to be preserved, explicit conversion methods are another solution:
String s = "" + 123; // Noncompliant String b = "" + true; // Noncompliant String f = "" + 3.14f; // Noncompliant
String s = Integer.toString(123); String b = Boolean.toString(true); String f = Float.toString(3.14f);