Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect usage of single | and & oprators #547

Open
abekornelis opened this issue Aug 5, 2024 · 0 comments
Open

Incorrect usage of single | and & oprators #547

abekornelis opened this issue Aug 5, 2024 · 0 comments
Labels
component-core Related to core components (java) Lang:Java

Comments

@abekornelis
Copy link
Collaborator

abekornelis commented Aug 5, 2024

In java single | and & operators denote a bitwise or and bitwise and respectively.
The double || and && are used to denote logical or and logical and.

In our java code the single, bitwise operators are used in many places where a double, logical operator is semantically required.
Current counts across our java base indicate we have 1463 && operators and 1413 single & operators.
Additionally we have 840 || operators and 449 single | operators.
That leaves us with almost 2000 instances to investigate, correct, and test.

For now: low priority. If we encounter any such coding issues during maintenance, please correct those coding errors while the routine needs to be fixed and tested anyway.

Example from https://stackoverflow.com/questions/1795808/and-and-or-in-if-statements
* & and | check both conditions no matter what
* && and || short-circuit
* && if 1st condition is false, does not check 2nd condition
* || if 1st condition is true, does not check 2nd condition
First bullet means & and | do not "short-circuit". The StackOverflow example demonstrates how one
can get a null pointer exception with a statement like "if (str != null & str.equals("hello))".

@abekornelis abekornelis added component-core Related to core components (java) Lang:Java labels Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component-core Related to core components (java) Lang:Java
Projects
None yet
Development

No branches or pull requests

1 participant