This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
[ JDK VERSION : 1.6.0_02 ] Override equals method led to != String comparison Steps: 1: create Ex:Test class public class Test { private String key; } 2:right click and invoke inset code--> equals() and hashcode() 3:select key variable to both equal and hashcode 4:click Generate and up with public class Test { private String key; @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Test other = (Test) obj; if (this.key != other.key && (this.key == null || !this.key.equals(other.key))) { return false; } return true; } @Override public int hashCode() { int hash = 5; hash = 89 * hash + (this.key != null ? this.key.hashCode() : 0); return hash; } }
There's nothing wrong on the generated code. It ends up calling equals() on the keys and returns true even in case when key1 != key2 as long as key1.equals(key2)
Comparing String using == or != (really bad idea ) (Editor give warnings ) see screen shot
Created attachment 50887 [details] Screen shot
Please note that the != is there mainly to cover key == null && other.key == null. The correctness of the test does not rely on comparing references, as if the two string are not the same instances, they are still compared using equals. *** This issue has been marked as a duplicate of 111441 ***