|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
The Java language supports various arithmetic operators for all floating-point and integer numbers. These include+(addition),-(subtraction),*(multiplication),/(division), and%(modulo). For example, you can use this Java code to add two numbers:Or you can use the following Java code to compute the remainder that results from dividingaddThis + toThisdivideThisbybyThis:divideThis % byThisThis table summarizes Java's binary arithmetic operations:
Operator Use Description +op1 + op2Adds op1andop2-op1 - op2Subtracts op2fromop1*op1 * op2Multiplies op1byop2/op1 / op2Divides op1byop2%op1 % op2Computes the remainder of dividing op1byop2
Note: The Java language extends the definition of the+operator to include string concatenation. You'll see more about this in Arrays and Strings.
The
+and-operators have unary versions that perform the following operations:
Operator Use Description ++opPromotes optointif it's abyte,short, orchar--opArithmetically negates opThere also are two short cut arithmetic operators,
++which increments its operand by 1, and--which decrements its operand by 1. Either++or--can appear before (prefix) or after (postfix) its operand. The prefix version,++op/--op, evaluates to the value of the operand after the increment/decrement operation. The postfix version,op++/op--, evaluates the value of the operand before the increment/decrement operation.Recall the
Sortprogram.
Let's look at how thepublic class Sort { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; for (int i = arrayOfInts.length; --i >= 0; ) { for (int j = 0; j < i; j++) { if (arrayOfInts[j] > arrayOfInts[j+1]) { int temp = arrayOfInts[j]; arrayOfInts[j] = arrayOfInts[j+1]; arrayOfInts[j+1] = temp; } } } for (int i = 0; i < arrayOfInts.length; i++) { System.out.print(arrayOfInts[i] + " "); } System.out.println(); } }Sortprogram uses--to control the outer of its two nested sorting loops. Here's the statement that controls the outer loop:The return value of the operationfor (int i = arrayOfInts.length; --i >= 0; ) { ... }--iis compared to 0 to determine whether to terminate the loop. Using the prefix version of--means that the last iteration of this loop occurs wheniis equal to 0. If we changed the code to use the postfix version of--, the last iteration of this loop occurs wheniis equal to -1, which is incorrect for this program.The other two loops in the program use the the postfix version of
++. In both cases, the version used doesn't really matter because the value returned by the operator isn't used for anything.The short cut increment/decrement operators are summarized in the following table:
Operator Use Description ++op++Increments opby 1; evaluates to the value of op before it was incremented++++opIncrements opby 1; evaluates to the value of op after it was incremented--op--Decrements opby 1; evaluates to the value of op before it was decremented----opDecrements opby 1; evaluates to the value of op after it was decremented
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |