76 Introduction to Java Applications Chapter 2 number1 (Web hosting solutions)

76 Introduction to Java Applications Chapter 2 number1 number2 45 72 Fig. 2.1414 Memory locations after storing values for number1and number2. Fig. After the program of Fig. 2.9 obtains values for number1and number2, it adds the values and places the sum into variable sum. The statement sum = number1 + number2; performs the addition and also replaces sum s previous value. After sum has been calculated, memory appears as shown in Fig. 2.15. Note that the values of number1 and number2 appear exactly as they did before they were used in the calculation of sum. These values were used, but not destroyed, as the computer performed the calculation. Thus, when a value is read from a memory location, the process is nondestructive. 2.7 Arithmetic Most programs perform arithmetic calculations. The arithmetic operators are summarized in Fig. 2.16. Note the use of various special symbols not used in algebra. The asterisk (*) indicates multiplication, and the percent sign (%) is the modulus operator, which is discussed shortly. The arithmetic operators in Fig. 2.16 are binary operators, because they each operate on two operands. For example, the expression sum+valuecontains the binary operator +and the two operands sumand value. Integer division yields an integer quotient; for example, the expression 7/4evaluates to 1, and the expression 17/5evaluates to 3. Note that any fractional part in integer division is simply discarded (i.e., truncated) no rounding occurs. Java provides the modulus operator, %, that yields the remainder after integer division. The expression x%yyields the remainder after xis divided by y. Thus, 7%4yields 3, and 17%5yields 2. This operator is most commonly used with integer operands, but also can be used with other arithmetic types. In later chapters, we consider many interesting applications of the modulus operator, such as determining if one number is a multiple of another. There is no arithmetic operator for exponentiation in Java. Chapter 5 shows how to perform exponentiation in Java. [Note: The modulus operator can be used with both integer and floating-point numbers.] number1 number2 sum 45 72 117 Fig. Fig. 2.15Memory locations after calculating the sum15 of number1and number2. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply