- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
Operators
You apply operators to integer variables to change a value or evaluate an expression for other use. The operators that apply to integers are described here in several related categories. Those not discussed in this section are covered in Chapter 5, "Using Expressions," and Chapter 6.
Arithmetic Operators
Arithmetic operators are used to perform standard mathematical operations on variables. These operators include:
+ |
Addition operator |
|
Subtraction operator |
* |
Multiplication operator |
/ |
Division operator |
% |
Remainder operator (gives the remainder of a division) |
The only operator in this list that you might not be familiar with is the remainder operator. The remainder of an operation is the remainder of the divisor divided by the dividend. In other words, in the expression 10 % 5, the remainder is 0 because 10 is evenly divisible by 5. However, the result of 11 % 5 is 1 because 11 divided by 5 is 2 remainder 1. This operator works equally well with floating-point operands. For example, 12.5 % 1.5 yields the result 0.5.
Although a familiar operator, the division operator exhibits some special behavior of which you must be aware. If the result of dividing two integers is not a whole number, Java always truncates the result (rather than rounding) to produce a whole number result. For example, 10 / 3 evaluates to 3, and 10 / 3 evaluates to 3.
You should also note that an ArithmeticException occurs if the right-side operand of either / or % evaluates to 0 (see "Catching and Throwing Exceptions" in Chapter 4, "Methods and Exceptions").
Listing 3.1 shows examples of the arithmetic operators in use with integers.
Listing 3.1 Examples Using Arithmetic Operators
int j = 60; int k = 24; int l = 30; int m = 11; int result = 0; result = j + k; // result = 84: (60 plus 24) result = result / m; // result = 7: (truncated result of 84 divided by 11) result = j - (2*k + result); // result = 5: (60 minus (48 plus 7)) result = k % result; // result = 4: (remainder 24 divided by 5)
Assignment Operators
The simplest assignment operator is the standard equals sign operator:
= |
Assignment operator |
This operator assigns the value of the expression on the right side to the variable on the left side. For example, x = y+3 evaluates the expression y+3 and assigns the result to x.
The assignment operator can also be used to make a series of assignments in a single statement. For example, x = y = z assigns the value of z to y and then the value of y to x. Notice that the assignments are performed from right to left.
The other Java assignment operators are known as arithmetic assignment operators. These operators provide a shortcut for assigning a value. When the previous value of a variable is a factor in determining the value that you want to assign, the arithmetic assignment operators are often more efficient:
+= |
Add and assign operator |
= |
Subtract and assign operator |
*= |
Multiply and assign operator |
/= |
Divide and assign operator |
%= |
Remainder and assign operator |
The arithmetic assignment operators are best explained by example. For instance, the following two lines are equivalent:
x += 5; x = x + 5;
These operators instruct the interpreter to use the operand on the left (x in this case) as the left-side operand in an expression with the right-side operand (5 in this case). The operator that precedes the equals sign determines the operation performed between the two entities. The result of this operation is then assigned to the operand on the left (x).
When using an arithmetic assignment operator, the operand on the left is evaluated only once. This distinction is unimportant if the operand is a simple variable like that shown in the preceding example. However, if that operand contains an expression that modifies another variable, the use of the arithmetic assignment operator might not produce the same results as that obtained without it. This is most likely to occur if the left operand contains an increment or decrement operator as discussed in the following section.
Listing 3.2 shows more examples of the assignment operators in use.
Listing 3.2 Examples Using Arithmetic Assignment Operators
byte j = 60; short k = 24; int l = 30; long m = 12L; long result = 0L; result += j; // result = 60: (0 plus 60) result += k; // result = 84: (60 plus 24) result /= m; // result = 7: (84 divided by 12) result -= l; // result = -23: (7 minus 30)) result = -result; // result = 23: (-(-23)) result %= m; // result = 11: (remainder 23 divided by 12)
Increment/Decrement Operators
The increment and decrement operators are used with a single operand (they are known as unary operators):
++ |
Increment operator |
|
Decrement operator |
For instance, the increment operator (++) adds one to its operand, as shown in the next line of code:
x++;
This is the same as:
x += 1;
The increment and decrement operators behave differently based on which side of the operand they are placed. When the increment operator is placed before its operand (for example, ++x), it is known as the pre-increment operator. This means that the increment occurs before the expression is evaluated. For example, after execution of the following two statements, both x and y have the value 6:
int x = 5; int y = ++x; // x and y are now both 6
This is because x is incremented before the right side is evaluated and assigned to y.
When the increment operator appears after its operand, it is known as the post-increment operator and the increment does not occur until after the expression has been evaluated. In the following modified lines of code, x is still incremented to 6 but only after y has been assigned the original value 5:
int x=5; int y = x++; // x is now 6 and y is 5
Similarly, you can use the decrement operator () in both pre-decrement and post-decrement fashion to subtract one from its operand.