DEV Community

Cover image for Numeric Data Types and Operations
Paul Ngugi
Paul Ngugi

Posted on

Numeric Data Types and Operations

Java has six numeric types for integers and floating-point numbers with operators +, -, *, /, and %.

Numeric 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 for numeric values, characters, and Boolean values.

Image description

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.

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

Methods for Scanner Objects

Image description

Here are examples for reading values of various types from the keyboard:

Scanner input = new Scanner(System.in);
System.out.print("Enter a byte value: ");
byte byteValue = input.nextByte();

System.out.print("Enter a short value: ");
short shortValue = input.nextShort();

System.out.print("Enter an int value: ");
int intValue = input.nextInt();

System.out.print("Enter a long value: ");
long longValue = input.nextLong();

System.out.print("Enter a float value: ");
float floatValue = input.nextFloat();
Enter fullscreen mode Exit fullscreen mode

If you enter a value with an incorrect range or format, a runtime error would occur.

Numeric Operators

The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (), multiplication (****), division (/), and remainder (%*).

When both operands of a division are integers, the result of the division is the quotient and the fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and –5 / 2 yields -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 remainder or modulo operator, yields the remainder after division. The operand on the left is the dividend and the operand on the right is the divisor. Therefore, 7 % 3 yields 1, 3 % 7 yields 3, 12 % 4 yields 0, 26 % 8 yields 2, and 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, and 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. Thus, you can use this property to determine whether a number is even or odd. 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:

(6 + 10) % 7 is 2

Enter fullscreen mode Exit fullscreen mode

The program obtains minutes and remaining seconds from an amount of time in seconds. For example, 500 seconds contains 8 minutes and 20 seconds.

Image description

The + and - operators can be both unary and binary. A unary operator has only one operand; a binary operator has two. For example, the - operator in -5 is a unary operator to negate number 5, whereas the - operator in 4 - 5 is a binary operator for subtracting 5 from 4.

Exponent Operations

The Math.pow(a, b) method can be used to compute a^b. 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 a^b (2^3). 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,

System.out.println(Math.pow(2, 3)); // Displays 8.0
System.out.println(Math.pow(4, 0.5)); // Displays 2.0
System.out.println(Math.pow(2.5, 2)); // Displays 6.25
System.out.println(Math.pow(2.5, -2)); // Displays 0.16
Enter fullscreen mode Exit fullscreen mode

Top comments (0)