< Day Day Up > |

## All about OperatorsFour different mathematical expressions are used in the An addition expression in Java uses the weight = weight + 10; This line sets the A subtraction expression uses the weight = weight - 15; This expression sets the A division expression uses the weight = weight / 3; The There's another expression you can use to find the remainder of an integer division. When the value of 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 To decrement the value of a variable by one, use the y--; This statement reduces You also can put the increment and decrement operators in front of the variable name, as in the following statements: ++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 However, When a postfixed operator is used on a variable inside an expression, the variable's value won't change until after the expression has been completely evaluated. The statement 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 There's some good news: You don't need to use the increment and decrement operators in your own programs. You can achieve the same results by using 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 The following order is used when working out an expression: 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, Comparisons will be discussed during Hour 7. The rest has been described during this hour, so you should be able to figure out the result of the following statements: int x = 5; int number = x++ * 6 + 4 * 10 / 2; These statements set the How does the computer come up with this total? First the increment operator is handled, and Because the original value of 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 If you want an expression to be evaluated in a different order, you can use parentheses to group parts of an expression that should be handled first. For example, the expression 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. |

< Day Day Up > |