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.

Bug 118783 - [60cat] Override equals method led to != String comparison
Summary: [60cat] Override equals method led to != String comparison
Status: RESOLVED DUPLICATE of bug 111441
Alias: None
Product: editor
Classification: Unclassified
Component: -- Other -- (show other bugs)
Version: 6.x
Hardware: PC Windows XP
: P3 blocker (vote)
Assignee: issues@editor
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-10-13 05:01 UTC by _ theanuradha
Modified: 2007-11-05 13:44 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Screen shot (15.49 KB, application/octet-stream)
2007-10-13 09:03 UTC, _ theanuradha
Details

Note You need to log in before you can comment on or make changes to this bug.
Description _ theanuradha 2007-10-13 05:01:43 UTC
[ 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;
    }

 
}
Comment 1 Petr Nejedly 2007-10-13 08:42:16 UTC
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)
Comment 2 _ theanuradha 2007-10-13 09:03:03 UTC
Comparing String using  == or  !=  (really bad idea ) (Editor give warnings ) see screen shot 
Comment 3 _ theanuradha 2007-10-13 09:03:36 UTC
Created attachment 50887 [details]
Screen shot
Comment 4 Jan Lahoda 2007-10-13 10:02:43 UTC
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 ***