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

20 lines
1.1 KiB
HTML

<html>
<body>
Reports <code>Collection.toArray()</code> calls that are not in the preferred style, and suggests applying the preferred style.
<p>There are two styles to convert a collection to an array:</p>
<ul>
<li>A pre-sized array, for example, <code>c.toArray(new String[c.size()])</code></li>
<li>An empty array, for example, <code>c.toArray(new String[0])</code></li>
</ul>
<p>In older Java versions, using a pre-sized array was recommended, as the reflection
call necessary to create an array of proper size was quite slow.</p>
<p>However, since late updates of OpenJDK 6, this call was intrinsified, making
the performance of the empty array version the same, and sometimes even better, compared
to the pre-sized version. Also, passing a pre-sized array is dangerous for a concurrent or
synchronized collection as a data race is possible between the <code>size</code> and <code>toArray</code>
calls. This may result in extra <code>null</code>s at the end of the array if the collection was concurrently
shrunk during the operation.</p>
<!-- tooltip end -->
<p>Use the inspection options to select the preferred style.</p>
</body>
</html>