Operators
You will learn about the following in this chapter:
All Java operators: what they are and how they are used.
Java Operator Precedence: what operators are evaluated first?
In the previous chapter you learned that computers think in terms of 1s and 0s, and that you can use those 1s and 0s to represent numbers, characters, and booleans. Then you learned that through Java you could create meaningful names that refer to regions of memory using variables. The next step is to take those variables and do something meaningful with them. In this chapter, you will address all the mathematical operations you can perform in Java on primitive data types.
Operators work in conjunction with operands, or the literal values or variables involved in the operation. There are unary operators, which are operators that operate on a single operand, as well as operators that operate on two or more variables.
Arithmetic Operators
Arithmetic operators refer to the standard mathematical operators you learned in elementary school: addition, subtraction, multiplication, and division.
Addition
Addition, as you would expect, is accomplished using the plus sign (+) operator. The form of an addition operation is
operand + operand
For example:
// Add two literal values int result = 5 + 5; // Add two variables int a = 5; int b = 6; int result = a + b; // Add two variables and a literal int result = a + b + 15;
An addition operation, can add two or more operands, whereas an operand can be a variable, a literal, or a constant.
Subtraction
Subtraction, again as, you would expect, is accomplished using the minus sign () operator. The form of a subtraction operation is
operand - operand
For example:
// Subtract a literal from a literal; the result is 5 int result = 10 - 5; // Subtract a variable from another variable; the result is -1 int a = 5; int b = 6; int result = a - b; // Subtract a variable and a literal from a variable // The result is 5 6 15 = -1 15 = -16 int result = a - b - 15;
A subtraction operation can compute the difference between two or more operands, where an operand can be a variable, a literal, or a constant.
Multiplication
Multiplication is accomplished using the asterisk (*) operator. The form of a multiplication operation is
operand * operand
For example:
// Multiply two literal values; result is 25 int result = 5 * 5; // Multiply two variables; result is 30 int a = 5; int b = 6; int result = a * b; // Multiply two variables and a literal // The result is 5 * 6 * 15 = 30 * 15 = 450 int result = a * b * 15;
A multiplication operation can multiply two or more operands, where an operand can be a variable, a literal, or a constant.
Division
Division is accomplished using the forward slash (/) operator. The form of a division operation is
operand / operand
For example:
// Divide a literal by a literal; result is 5 int result = 10 / 2; // Divide a variable by another variable; result is 3 int a = 15; int b = 5; int result = a / b;
When dividing integer types, the result is an integer type (see the previous chapter for the exact data type conversions for mathematical operations). This means that if you divide an integer unevenly by another integer, it returns the whole number part of the result; it does not perform any rounding. For example, consider the following two operations that both result to 1.
int result1 = 10 / 6; // Float value would be 1.6666 int result2 = 10 / 9; // Float value would be 1.1111
Both result1 and result2 resolve to be 1, even though result1 would typically resolve to 2 if you were rounding off the result. Therefore, be cognizant of the fact that integer division in Java results in only the whole number part of the result, any fractional part is dropped.
When dividing floating-point variables or values, this caution can be safely ignored. Floating-point division results in the correct result: The fractional part of the answer is represented in the floating-point variable.
float f = 10.0f / 6.0f; // result is 1.6666 double d = 10.0 / 9.0; // result is 1.1111
Note the appearance of the f following each literal value in the first line. When creating a floating-point literal value (a value that has a fractional element), the default assumption by the compiler is that the values are double. So, to explicitly tell the compiler that the value is a float and not a double, you can suffix the value with either a lowercase or uppercase F.
Modulus
If integer division results in dropping the remainder of the operation, what happens to it? For example if you divide 10 by 6:
int i = 10 / 6;
The Java result is 1, but the true result is 1 Remainder 4. What happened to the remainder 4?
Java provides a mechanism to get the remainder of a division operation through the modulus operator, denoted by the percent character (%). Although the previous example had a result of 1, the modulus of the operation would give you that missing 4. The form of a modulus operation is
operand % operand
For example:
int i = 10 / 6; // i = 1 int r = 10 % 6; // r = 4
Similar to the other arithmetic operators in this chapter, the modulus of an operation can be performed between variables, literals, and constants.