Files
openide/java/java-impl/resources/inspectionDescriptions/UnnecessaryToStringCall.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

30 lines
1.5 KiB
HTML

<html>
<body>
Reports calls to <code>toString()</code> that are used in the following cases:
<ul>
<li>In string concatenations</li>
<li>In the <code>java.lang.StringBuilder#append()</code> or <code>java.lang.StringBuffer#append()</code> methods</li>
<li>In the methods of <code>java.io.PrintWriter</code> or <code>java.io.PrintStream</code></li>
<li>in the methods <code>org.slf4j.Logger</code></li>
</ul>
<p>In these cases, conversion to string will be handled by the underlying library methods,
and the explicit call to <code>toString()</code> is not needed.
Removing redundant <code>toString()</code> calls can occasionally even improve performance and reduce object allocations.</p>
<p>Example:</p>
<pre><code>
System.out.println(this.toString())
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
System.out.println(this)
</code></pre>
<!-- tooltip end -->
<p>
Note that without the <code>toString()</code> call, the code semantics might be different: if the expression is null,
then the <code>null</code> string will be used instead of throwing a <code>NullPointerException</code>.</p>
<p>Use the <b>Report only when qualifier is known to be not-null</b> option to avoid warnings for the values that could potentially be null.
Removing the explicit <code>toString()</code> in these cases will change the runtime semantics
from throwing a <code>NullPointException</code> to silently accepting the value when it is <code>null</code>.
</p>
</body>
</html>