This chapter goes into more depth about relational and logical operators. You will have to use these concepts to write complex programs that other people can read and follow.
3. Numeric Data and Operators
3.1. Numeric Operations
Numeric operators
The operations that can be done on numeric data include the standard algebraic operations: addition (+), subtraction (-), multiplication (*), division (/), as well as the modulus (%) operator. Note that in Java, the multiplication symbol is * and not the x. The arithmetic operators are binary operators, meaning that they each take two operands. Table 5.4 compares expressions involving the Java operators with their standard algebraic counterparts.
TABLE 5.4 | The standard arithmetic operators in Java | ||
Operation | Operator | Java | Algebra |
Addition | + | x+2 | x+2 |
Subtraction | - | m -2 | m-2 |
Multiplication | * | m*2 | 2m or 2 x m |
Division | / | x/y | x\( \div\) or \(\frac{x}{y}\) |
Modulus | to | x%y | A modulo y (for integers x and y) |
Although these operations should seem familiar, there are some important differences between their use in algebra and their use in a Java program. Consider the following list of expressions:
3 / 2 |
=> | value | 1 | An integer result | |
3.0 / 2.0 |
=> | value | 1.5 |
A floating - point | result |
3 / 2.0 |
=> | value | 1.5 |
A floating - point | result |
3.0 / 2 |
=> | value | 1.5 |
A floating - point | result |
Integer division gives an integer result
In each of these cases we are dividing the quantity 3 by the quantity 2. However, different results are obtained depending on the type of the operands involved. When both operands are integers, as in (3/2), the result must also be an integer. Hence, (3/2) has the value 1, an integer. Because integers cannot have a fractional part, the 0.5 is simply discarded. Integer division gives an integer Integer division (/) always gives an integer result. Thus, the value of (6/2) result is 3 and the value of (7/2) is also 3. Because 3.5 is not an integer, the result of dividing 7 by 2 cannot be 3.5.
JAVA DEBUGGING TIP Integer Division. A common source of error among beginning programmers is forgetting that integer division always gives an integer result.
On the other hand, when either operand is a real number, as in the last three cases, the result is a real number. Thus, while the same symbol (/) is used for dividing integers and real numbers, there are really two different operations involved here: integer division and floating-point division. Using the same symbol (/) for different operations (integer division and real division) is known as operator overloading. It is similar to method overloading, which was discussed in Chapter 3.
Modular arithmetic
What if you want to keep the remainder of an integer division? Java Modular arithmetic provides the modulus operator (%), which takes two operands. The expression (7 % 5) gives the remainder after dividing 7 by 5 - 2 in this case.
In general, the expression (m % n) (read m mod n) gives the remainder after m is divided by n. Here are several examples:
7% 5 | --=> | 7 mod 5 equals 2 |
5% 7 | ==> | 5 mod 7 equals 5 |
-7 % 5 | ==> | -7 mod 5 equals -2 |
7 % -5 | ==> | 7 mod -5 equals 2 |
The best way to interpret these examples is to perform long division on the operands keeping both the quotient and the remainder. For example, when you do long division on -7 ÷ 5, you get a quotient of -1 and a remainder of -2. The quotient is the value
of -7/5 and the remainder is the value of -7%5. When you do long division on 7÷ -5, you get a quotient of -1 and a remainder of 2. The quotient is the value of 7/ - 5 and the remainder is the value of 7% - 5.
We will encounter many practical uses for the modulus operator in our programs. For a simple example, we use it when we want to determine whether an integer is even or odd. Numbers that leave a 0 remainder when divided by 2 are even:
More generally, we could use the mod operator to define divisibility by 3, 4, 10, or by any number.if (N % 2 == 0)
System . out . println (N + " is even" );
Numeric Promotion Rules
Expressions have a type
Java is considered a strongly typed language because all expressions in Java, such as (3/2), have a type associated with them. In cases where one arithmetic operand is an integer and one is a floating-point number, Java promotes the integer
into a floating-point value and performs a floating-point operation.
Promotion is a matter of converting one type to another type. For example, in the expression (5 + 4.0), the value 5 must be promoted to 5.0 before floating-point addition can be performed on (5.0 + 4.0). Generally speaking, automatic promotions such as these are allowed in Java whenever it is possible to perform the promotion without loss of information. Because an integer (5) does not have a fractional component, no information will be lost in promoting it to a real number (5.0). On the other hand, you cannot automatically convert a real number (5.4) to an integer (5) because that might lead to loss of information. This leads to the following rule:
JAVA LANGUAGE RULE Integer Promotion. In an operation that contains an integer and a floating-point operand, the integer is promoted to a floating-point value before the operation is performed.
This rule is actually an instance of a more general rule, for whenever an expression involves operands of different types, some operands must be converted before the expression can be evaluated. Consider the following example:
byte n = 125;
short m = 32000;
n * m;
In this case, (n * m) involves two different integer types, byte and short. Before evaluating this expression Java must first promote the byte to a short and carry out the operation as the multiplication of two shorts. Conversion of short to byte would not be possible because there's no way to represent the value 32000 as a byte.
Promotion is automatic
It is important to note that this conversion rule applies regardless of the actual values of the operands. In applying the rule, Java looks at the operand's type, not its value. So even if m were assigned a value that Promotion is automatic could be represented as a byte (for example, 100), the promotion would still go from smaller to larger type. This leads to following the general rule:
JAVA LANGUAGE RULE Type Promotion. In general, when two different types are involved in an operation, the smaller type – the one with fewer bits – is converted to the larger type before the operation is performed. To do otherwise would risk losing information.
Table 5.5 Java promotion rules for mixed arithmetic operators. If two rules apply, choose the one that occurs first in this table.
If either operand is | The other is promoted to |
---|---|
double | double |
float | float |
long | long |
byte or short | int |