Previous Next |

## All about OperatorsFour different mathematical expressions are used in the weight = weight + 10; This line sets the weight = weight - 15; This expression sets the weight = weight / 3; The int remainder = 245 % 3; A multiplication expression uses the weight = weight + (weight * 12); The ## Incrementing and Decrementing a VariableA common task in programs is to change the value of a variable by one. You can increase the value by one, which is called incrementing the variable, or decrease the value by one, which is decrementing the variable. There are operators to accomplish each of these tasks. To increment the value of a variable by one, use the x++; This statement adds one to the value stored in the y--; This statement reduces ++x; --y; Putting the operator in front of the variable name is called prefixing, and putting it after the name is called postfixing. You probably have many cherished memories of grade school language lessons, when you learned about prefixes such as "pre-", "extra-", and "sub-". A prefixed operator is like a prefix in a word—it comes first. Postfixed operators lag behind. (If your memories of those classes are not-so-cherished, you must not have sat behind Mary Beth Farkas.) Although it might seem redundant for Java to include both prefixed and postfixed operators, the difference becomes important when you use the increment and decrement operators inside an expression. Consider the following statements: int x = 3; int answer = x++ * 10; What does the int answer = x * 10; x++; The opposite is true of prefixed operators. If they are used on a variable inside an expression, the variable's value changes before the expression is evaluated. Consider the following statements: int x = 3; int answer = ++x * 10; This does result in the x++; int answer = x * 10; At this point, you might be ready to say, "Prefixing, postfixing, incrementing, decrementing—let's call the whole thing off!" It's easy to become exasperated with the
## Operator PrecedenceWhen you are using an expression with more than one operator, you need to know what order the computer will use as it works out the expression. Consider the following statements: int y = 10; x = y * 3 + 5; Unless you know what order the computer will use when working out the math in this expression, you cannot be sure what the - Incrementing and decrementing take place first.
- Multiplication, division, and modulus division occur next.
- Addition and subtraction follow.
- Comparisons take place next.
- The equal sign (
`=`) is used to set a variable's value.
Because multiplication takes place before addition, you can revisit the previous example and come up with the answer: int y = 10; x = y * 3 + 5; In the last statement, int x = 5; int number = x++ * 6 + 4 * 10 / 2; These statements set the int number = 5 * 6 + 4 * 10 / 2; Now, multiplication and division are handled from left to right. First, 5 is multiplied by 6, 4 is multiplied by 10, and that result is divided by 2 ( int number = 30 + 20; This expression results in the x = 5 * (3 + 2); In this case, the expression within the parentheses is handled first, so the result equals 25. You can use parentheses as often as needed in a statement. |

Previous Next |