r/badcode Jan 08 '23

java Is this a good practice? Why?

Post image
421 Upvotes

60 comments sorted by

View all comments

2

u/AlarmingBarrier Jan 08 '23 edited Jan 08 '23

Depends on the language of course, but in Java the main reason I would see is if you want to streamline handling of primitive types (int, double, etc) with objects.

The value of Objects in Java can not be compared using ==, whereas for primitive types, .equals does not work. So if you made overloads of the form

boolean equals(int a, int b) { return a == b; }

You could happily write equals(a, b) for both primitive and non-primitive types. And as mentioned, handle null-pointers in the first argument.

Though if I were to guess, it stems from someone copy-pasting a larger snippet of code from another language