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.
Summary: | Incorrect "Comparing string using ==" hint when using constants | ||
---|---|---|---|
Product: | java | Reporter: | dusty <dusty> |
Component: | Hints | Assignee: | Svata Dedic <sdedic> |
Status: | NEW --- | ||
Severity: | normal | ||
Priority: | P3 | ||
Version: | 7.3.1 | ||
Hardware: | PC | ||
OS: | Linux | ||
Issue Type: | ENHANCEMENT | Exception Reporter: |
Description
dusty
2013-09-19 15:59:35 UTC
Comparison using == can be dangerous even in your case; each String compile time constant is implicitly interned. As a result, if there's a piece of code, which happens to call x = v.intern() on String v containing "dummy", STR1 and x will receive the same reference and the comparison in your example might evaluate to true inappropriately. In case you *want* to have String constant and compare by reference, use explicit new String("dummy") to work around the implicit intern. This enhancement would probably require a NB specific source-only annotation on the declared element; other possibility would be to @SuppressWarnings() for the unwanted hint code - should be one of the options offered in hints. Thanks for the insight. I don't know much about string interning: am I right to say that anyway, if the content of the string is different, it's not possible to have v == s? In understand that using two different constants with the same value is certainly a good way to look for problems :) The hint could look out for @SuppressWarnings on the constant. Placing specific @SuppressWarning on a constant would also declare that the programmer intents to work with the exact instance rather than with value. |