mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
35 lines
1.2 KiB
HTML
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> |