Tag Archives for boxing
Boxing with Java
A bug was recently identified in a project I’m working on. Check the following code out:
|
1 2 3 4 5 6 7 |
private void addItem(Integer itemCode, Item itemFromDb){ if(itemCode == itemFromDb.getItemCode()){ //foo } else{ //bar } } |
Naughty naughty. I had done a very silly thing and used == to test equality of Integer objects! As we all know, when comparing equality on objects we should be using .equals(). However, this bug was intermittent. Comparing [...]
Posted in Programming, Technology
Also tagged autoboxing, autounboxing, java, primitive, programming, software, unboxing, wrapper
15 Comments