< Day Day Up > |
All about OperatorsFour different mathematical expressions are used in the Elvis program—to add weight to Elvis, subtract weight from Elvis, divide Elvis, and finish off with some multiplied Elvii. Each of these expressions uses symbols (+, -, *, /, and %) called operators. You will be using these operators to crunch numbers throughout your Java programs. An addition expression in Java uses the + sign, as in Line 7 of your program: weight = weight + 10; This line sets the weight variable equal to its current value plus 10. Because the weight was set to 250 when it was created, Line 7 changes weight to 260. A subtraction expression uses the - sign, as in Line 10: weight = weight - 15; This expression sets the weight variable equal to its current value minus 15. The weight variable is now equal to 245. A division expression uses the / sign, as in Line 14: weight = weight / 3; The weight variable is set to its current value divided by 3 and rounded down because weight is an integer. The weight variable is now equal to 81. There's another expression you can use to find the remainder of an integer division. When the value of the weight variable was divided by 3 in Line 14, a remainder of 2 was discarded in order for weight to remain as an integer value. To find a remainder from a division expression, use the % operator. You can use the following statement to find the remainder of 245 divided by 3: int remainder = 245 % 3; A multiplication expression uses the * sign. Line 17 uses a multiplication expression as part of a more complicated statement: weight = weight + (weight * 12); The weight * 12 part of the expression multiplies weight by 12. The full statement takes the current value of weight and adds it to weight multiplied by 12. This example shows how more than one expression can be combined in a statement. The result is that weight becomes 1,053—in other words, 81 + (81 * 12). 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 ++ operator, as in the following statement: x++; This statement adds one to the value stored in the x variable. To decrement the value of a variable by one, use the -- operator: y--; This statement reduces y by one. 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 answer variable equal after these statements are handled? You might expect it to equal 40—which would be true if 3 was incremented by 1, which equals 4, and then 4 was multiplied by 10. However, answer is set to 30. The reason is that the postfixed operator was used instead of the prefixed operator. 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 does the same thing, in the same order, as the following two statements: 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 answer variable being equal to 40. The prefixed operator causes the value of the x variable to be changed before the expression is evaluated. The statement int answer = ++x * 10 does the same thing, in order, as these statements: 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 ++ and -- operators, because they're not as straightforward as many of the concepts you'll encounter in this book. 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 + and – operators. Incrementing and decrementing are useful shortcuts, but taking the longer route in an expression is fine, too.
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 x variable will be set to. It could be set to either 35 or 80, depending on whether y * 3 is evaluated first or 3 + 5 is evaluated first. The following order is used when working out an expression:
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, y is multiplied by 3 first, which equals 30, and then 5 is added. The x variable is set to 35. 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 number variable equal to 50. How does the computer come up with this total? First the increment operator is handled, and x++ sets the value of the x variable to 6. However, make note of the fact that the ++ operator is postfixed after x in the expression. This means that the expression is evaluated with the original value of x. Because the original value of x is used before the variable is incremented, the expression becomes the following: 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 (4 * 10 / 2). The expression becomes the following: int number = 30 + 20; This expression results in the number variable being set to 50. 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; would normally cause x to equal 17 because multiplication is handled before addition. However, look at a modified form of that 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 > |