Why does Netbeans offer me "Flip operands of binary operators" in my Java code

Netbeans often assumes that I “flip operands of a binary operator” when I perform math calculations. For example, in the following line of code:

change = 100 - price; quarters = change / 25; dimes = change % 25 / 10; nickels = change % 25 % 10 / 5; pennies = change % 25 % 10 % 5; 

Netbeans makes a sentence for each mathematical symbol (therefore, it does this three times in the Penny line.

I'm not sure I understand why he makes this proposal. If I had to flip the operands while doing the division, I would get a different result (if “flip” means what I think it does, that switches the order of the two values). Why does it offer?

+7
java netbeans operands flip
source share
1 answer

Your idea that this is a Netbeans function for quickly switching operands is correct. This is one of the relatively few “suggestions / actions” available in Java Hints ( http://wiki.netbeans.org/Java_Hints ), unlike the more numerous “hints / checks”.

In Netbeans 8.2, I checked that when you press Alt + Enter in the pennyies line, in your fragment there is a menu option "Flip operands"% "(can change semantics)." In fact, this can cause multiple such menu options, because there are several binary operators. If you decide to flip the operands, the prompt remains, and you can flip them over and over again over and over again by the same means.

Obviously, Netbeans is smart enough to at least be aware that flipping operands for this type of operator can change semantics (although this does not mean behavior). For '==' it does not carry this warning.

0
source share

All Articles