FindBugs recognizes the following bug in my code:
Suspicious comparison of Long references
I have tried Googling this particular error but with no luck. As far as I know comparing fields of data type Long
should be done with ==
operator.
if (materialDefinition.getId() == definitionToRemoveFromClass.getId()) {
//...
}
I am certain both methods .getId()
return a Long
.
Is this really an issue and if so, how do I fix it?
Is this really an issue and if so, how do I fix it?
Yes, you're comparing references not values, so two different Long
instances with identical values will result in that comparison being false
. You should use Long#equals()
instead:
final Long id1 = materialDefinition.getId();
final Long id2 = definitionToRemoveFromClass.getId();
if (id1.equals(id2)) {
//...
}
If getId()
can return null
, then also be sure to include the appropriate null
check (although getId()
doesn't sound like a method that should be returning null
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With