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

35 lines
1.2 KiB
HTML

<html>
<body>
Reports construction of (temporary) new objects inside <code>equals()</code>, <code>hashCode()</code>, <code>compareTo()</code>, and
<code>Comparator.compare()</code> methods.
<p>
Besides constructor invocations, new objects can also be created by autoboxing or iterator creation inside a
<code>foreach</code> statement.
This can cause performance problems, for example, when objects are added to a <code>Set</code> or <code>Map</code>,
where these methods will be called often.
</p>
<p>
The inspection will not report when the objects are created in a <code>throw</code> or <code>assert</code> statement.
</p>
<p><b>Example:</b></p>
<pre><code>
class Person {
private String name;
private int age;
public boolean equals(Object o) {
return Arrays.equals(new Object[] {name, age}, new Object[] {((Foo)o).name, ((Foo)o).age});
}
public int hashCode() {
return (name + age).hashCode();
}
}
</code></pre>
<p>
In this example, two additional arrays are created inside <code>equals()</code>, usages of <code>age</code> field require boxing,
and <code>name + age</code> implicitly creates a new string.
</p>
<!-- tooltip end -->
</body>
</html>