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.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

StringOperationCanBeSimplified
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Verbose or redundant code constructs

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.

Use the Do not report single argument substring() calls option below to not report code like the following.

stringBuilder.append(string.substring(5));

which can be replaced with the following.

stringBuilder.append(string, 5, string.length());

New in 2018.1

Inspection options

Here you can find the description of settings available for the Redundant 'String' operation inspection, and the reference of their default values.

Do not report String constructor calls

Not selected

Do not report redundant single argument substring() calls

Default: Selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

Java, 242.22892

Last modified: 11 September 2024