Inspectopedia Help

Redundant 'String' operation

Reports redundant calls to String constructors and methods like toString() or substring() that can be replaced with a simpler expression.

For example, calls to these methods can be safely removed in code like "string".substring(0), "string".toString(), or new StringBuilder().toString().substring(1,3).

Example:

System.out.println(new String("message"));

After the quick-fix is applied:

System.out.println("message");

Note that the quick-fix removes the redundant constructor call, and this may affect String referential equality. If you need to preserve it, even though it is considered bad practice, suppress the warning or use the inspection setting to ignore redundant String constructor calls.

Use the Do not report String constructor calls option below to not report code like the example above. This will avoid changing the outcome of String comparisons with == or != after applying the quick-fix in code that uses new String() calls to guarantee a different object identity.

New in 2018.1

Inspection options

Option

Type

Default

Do not report String constructor calls

Checkbox

false

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3

Plugin:

Java, 233.SNAPSHOT

Last modified: 13 July 2023