white hexagonal glass window ceiling with coffee crema cream bubbles

1.11 Java | Numeric Data Type Operations

Numeric Data Types

Every data type has a range of values. The compiler allocates memory space for each variable or constant according to its data type. Java provides eight primitive data types: byte, boolean, char, double, float, int, long, short.

Java uses four types for integers: byte, short, int, and long. Choose the type that is most appropriate for your variable. For example, if you know an integer stored in a variable is within a range of a byte, declare the variable as a byte. For simplicity and consistency, we use int for integer.

Java uses two types for floating-point numbers: float and double. The double type is twice as big as float, so the double is known as double precision and float as single precision. Normally, you should use the double type, because it is more accurate than the float type.

Reading Numbers from the Keyboard

You know how to use the nextDouble() method in the Scanner class to read a double value from the keyboard (if not, check out the Reading Input from Console tutorial). You can also use other methods to read numbers of the byte, short, int, long, and float type. Like these ones:

Here are a few examples showing how to values of various types are read from the keyboard:

If you enter a value with an incorrect range or format, a runtime error would occur. For example, you enter a value 128 for byte byteValue = input.nextByte();, an error would occur because 128 is out of range for a byte type integer (byte types only accept 1s and 0s i.e. 10010).

Numeric Operators

The operators for numeric data types include the standard arithmetic operators: addition + subtraction - multiplication * division / and remainder %, as shown below. The operands are the values operated by an operator.

When both operands of a division are integers, the result of the division is the quotient; the fractional part is truncated. For example, 5 / 2 yields 2, not 2.5; -5 / 2 yeilds -2, not -2.5. To perform a float-point division, one of the operands must be a floating-point number. For example, 5.0 / 2 yields 2.5.

The % operator, known as the reminder or modulo operator, yields the remainder after division. The operand on the left is the dividend. The operand on the right is the divisor. Therefore:

  • 7 % 3 yields 1
  • 3 % 7 yields 3
  • 12 % 7 yields 3
  • 12 % 4 yields 0
  • 26 % 8 yields 2
  • 20 % 13 yields 7
Java Numeric Operators - the remainder %
20 % 13 yields 7

The % operator is often used for positive integers, but it can also be used with negative integers and floating-point values. The remainder is negative only if the dividend is negative. For example:

  • -7 % 3 yields -1
  • -12 % 4 yields 0
  • -26 % -8 yields –2
  • 20 % -13 yields 7

Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1 or -1.Thus, you can use this property to determine whether a number is even or odd.

% Remainder Example 1 – Weekday

If today is Saturday, it will be Saturday again in 7 days. Suppose you and your friends are going to meet in 10 days. What day is in 10 days? You can find that the day is Tuesday using the following expression:

Java expression showing the significance of the remainder operator
Day 1 = Monday,…, Day 7 = Sunday. So starting on Saturday (day 6) add 10 days. 16 % 7 days = 2. Day 2 = Tuesday.

% Remainder Example 2 – Display Time

In this example, I will be showing a program that obtains minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.

Output:

Java Display Time minutes seconds remainder operator

Explanation: The nextInt() method reads an integer for seconds. Then the seconds are divided by 60 (seconds / 60) to obtain the number of minutes. Then the seconds goes through the remainder operator (seconds % 60) to find the number of seconds remaining after divided by 60.

Exponent Operations

The Math.pow(a, b) method can be used to compute ab. The pow method is defined in the Math class in the Java API. You invoke the method using the syntax Math.pow(a, b) (e.g., Math.pow(2, 3)), which returns the result of ab (23). Here, a and b are parameters for the pow method and the numbers 2 and 3 are actual values used to invoke the method. For example,


What's Your Opinion?