Java Numeric Data Types and Operations

Numeric Data Types and Operations in Java
In programming, every data type has a range of values. The compiler allocates memory space used to store each variable or constant according to the data type assigned to it. Java provides eight primitive data types for numeric values, characters, and Boolean values. Numeric data types, their ranges, and their storage sizes:

Note: "IEEE 754 is a standard approved by the Institute of Electrical and Electronics Engineers for representing floating-point numbers on computers. This standard has been widely adopted in engineering. Java has adopted the 32-bit IEEE 754 for the float type and the 64-bit IEEE 754 for the double type. The IEEE 754 standard also defines special values and operations on 'Special Floating-Point Values.'" Integers in Java: "Java uses four types for integers, these are; 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 byte, declare the variable as a byte." Floating point numbers: Java uses two types for floating-point numbers, these are; float and double. The double type is twice as big as float. So, the double type is known as double precision, while float is known as single precision. It is advisable that you should use the double type because it is more accurate than the float type.

Numeric Operators in Java
Just like other programming languages, Java uses operators to perform numeric operations. The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (–), multiplication (*), division (/), and remainder (%). Numeric Operators: NB: 1. The result of an integer division is an integer. The fractional part is automatically truncated. Example: 10/4 yields 2, not 2.5, and -5 / 2 yields -2, not -2.5 2. The % operator returns the remainder after the division operation. The left-hand operand is the dividend, and the right-hand operand is the divisor. Therefore, 7 % 3 yields 1, 12 % 5 yields 2, 24 % 8 yields 0, and 20 % 12 yields 8. The % operator is mostly used for positive integers though it can also be used with negative integers and floating-point values. The remainder is negative only if the dividend is negative. Example, -7 % 3 yields -1, -12 % 3 yields 0, -26 % -7 yields -5, and 20 % -17 yields -3. The remainder is very useful in programming in that it can be used to determine whether a number is even or odd (an even number % 2 is always 0, and an odd number % 2 is always 1), and in determining leap years. Suppose today is Saturday, and you have a class in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression: //if saturday is the sixt day in a week then myDay = (6 + 10)% 7 // the remainder is 2, 2 is the second day in a week (tuesday) Example: TimeDisplay.java import javax.swing.JOptionPane; public class TimeDisplay { public static void main(String[] args) { int seconds = 3200; int minutes = seconds / 60; int remainingSeconds = seconds % 60; JOptionPane.showMessageDialog(null,        seconds + " seconds is " + minutes +        " minutes and " + remainingSeconds        + " seconds"); } }

Line 6 performs integer devision by deviding seconds by 60 and therefore obtaining minutes. Line 7 (seconds % 60) obtains the remainder of seconds after having obtained minutes. The operators + and - operators can be both unary and binary. A unary operator has only one operand, while a binary operator has two operands. When placed before a single value or when occuring inside an expression and they appear following each other before, the symbol preceding the value becomes a unary operator. They define the direction of an operation (number line)

Example, the - operator in -7 can be considered a unary operator to negate number 5, whereas the - operator in 4 - 5 is a binary operator for subtracting 5 from 4. Also 4--5 has the first occurance of - as a binary operator and the second - as a unary operator negating 5.

NB: 1. Calculations that involve floating-point numbers are normaly approximations, this is because these numbers are not stored with complete accuracy. Example: System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1); "outputs 0.5000000000000001, not 0.5, while" System.out.println(1.0-0.9); "outputs 0.09999999999999998, and not 0.1."

2. Integers are stored precisely. Therefore, calculations with integers yield a precise integer result.

Java Numeric Literals
In programming, a literal is a constant value that appears or is specified directly in a program. Example: int i = 24; long k = 9800000; double d = 25.340; //The values 24, 9800000, and 25.340 are literals

Java Integer Literals
An integer literal can be assigned to an integer variable as long as it can fit into the variable. If the literal is too large for the variable to be able to hold, then a compilation error would occur. The statement byte b = 129, for example, would cause a compilation error, because 129 cannot be stored in a variable of the byte type. (Note that the range for a byte value is from -128 to 127.) An integer literal is assumed to be of the int data type, whose value is between –231 (-2147483648) and 231–1 (2147483647). The statement System.out.println (2147483649), for example, would cause a compilation error, because 2147483649 is too long as an int value. To indicate an integer literal of the long type, append the letter L or l to it (e.g., 2147483649L). It is preferrable to use L because l (lowercase L) can easily be confused with 1 (the digit one) or with upper case i. Since 2147483649 exceeds the range for the int data type, it must be denoted as long in the form 2147483649L.

NB: "By default, an integer literal is a decimal number. To denote an octal integer literal, use a leading 0 (zero), and to denote a hexadecimal integer literal, use a leading 0x or 0X (zero x). Example:" // code to display the decimal value 65535 for hexadecimal number FFFF. System.out.println(oxFFFF);

Floating-Point Literals as applied in Java
Floating-point literals are written with a decimal point inclussive. A floating-point literal is treated as a double type value (with double precission) by default.In this case, 5.0 is considered a double value, and not a float value. You can make a number a float number by appending the letter f or F to it, and you can make a number a double by appending the letter d or D to it. Example: 700.2f or 700.2F for a float number 700.2d or 700.2D for a double number.

NB: The double type values are more accurate than the float type values. Example: System.out.println("10.0 / 30.0 is " + 10.0 / 30.0); "displays 1.0 / 3.0 is 0.3333333333333333." System.out.println("1.0F / 3.0F is " + 1.0F / 3.0F); "displays 1.0F / 3.0F is 0.33333334."

Scientific Notations in Java
The scientific notation can also be used in representing floating-point literals. Example: 1.23456e+2, is the same as 1.23456e2, is equivalent to 1.23456 x 102 = 123.456 1.23456e–2 is equivalent to 1.23456 x 10-2 = 0.0123456. E (or e) represents an exponent and can be specified either in lowercase or uppercase.

NB: "The float and double data types are used to represent numbers with a decimal point (real numbers). These numbers are are called floating point because they are stored into scientific notation. When a number such as 530.534 is converted into scientific notation such as 53.053e1, its decimal point is moved (floated) to a new position."

Java Arithmetic Expressions
When coding, writing numeric expressions in Java involves a straight forward process translation of an arithmetic expression bt using the Java operators. Example of arithmetic expression: translatation into a Java program expression: m = (8 + 4 * x) / 5 – 10 * (y - 7) * (a + b + c) / x + 9 * (4 / x + (9 + x) / y); In Java, the numeric operators in an expression are applied the same way as they are applied in an arithmetic expression. Operators within pairs of parentheses are the first to be evaluated. In In some expressions, parentheses can be inside others, in such a case, they are said to be nested. I cases where parenthesis are nested,then the expression in the inner parentheses is evaluated first. Multiplication, division, and remainder operations are applied next. If an expression contains several multiplication, division, and remainder operators, they are evaluated from left to right. Addition and subtraction operators are applied last. Like wise if an expression contains several addition and subtraction operators, their execution begins from left to right. Program that converts a Fahrenheit degree to Celsius using the formula celsius = (fahrenheit - 32). public class FahrenheitToCelsius { public static void main(String[] args) { double fahrenheit = 100; // We assign the value 100 to the variable farenheit double celsius = (5.0 / 9) * (fahrenheit - 32);//Compute celsius System.out.println("Fahrenheit " + fahrenheit + " is " +       celsius + " in Celsius"); } } You need to be careful when applying divisions in Java. This is because in Java, division of two integers yields an integer. On line 4 we use 5.0 / 9 (floating point) instead of 5 / 9, this is because 5 / 9 yields 0 in Java (integer division).

Java Shorthand Operators
Very often the current value of a variable is used, modified, and then reassigned back to the same variable. For example, the statement below adds the current value of x with 10 and assigns the result back to x. x = x + 10; Java makes programming code shorter by allowing you to combine assignment and addition operators using a shorthand operator. For example, the above statement can be written as: x += 10; The += is reffered to as the addition assignment operator. Shorthand Operators: There are two additional shorthand operators used for incrementing and decrementing a variable by 1. These two operators are ++ and --. These operators can be used in prefix or suffix notation. Increment and Decrement Operators shorthand operators: When the operator occurs before (prefixed to) the variable, then depending on the symbol of the operator, the variable is incremented or decremented by 1, then the new value of the variable is returned. If the operator is placed after (suffixed to) the variable, the original old value of the variable is returned, then the variable is incremented or decremented by 1. Therefore, the prefixes ++x and --x are referred to, respectively, as the preincrement operator and the predecrement operator; and the suffixes x++ and x-- are referred to as the postincrement operator and the postdecrement operator respectively. The prefix form of ++ (or --) and the suffix form of ++ (or --) are the same effect only if they are used in isolation, but they cause different effects when used in an expression. Example: Here is another example: double x = 1.0; double y = 6.0; double z = x–– + (++y); After all three lines are executed, y becomes 7.0, z becomes 8.0, and x becomes 0.0. The increment operator ++ and the decrement operator –– can be applied to all integer and floating-point types. These operators are often used in repetitive (iteration) statements.

NB: "1. Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables or the same variable multiple times, such as: int m = ++n +n." 2. Just like the assignment operator (=), the operators (+=, -=, *=, /=, %=, ++, and --) can also be used to form an assignment statement as well as an expression. Example: x = 2; <font color="green"// Assignment statement System.out.println(x = 2); // expression statement //x = 2 is a statement in the first line and is an expression in the second line.

"If a statement is used as an expression, it is called an expression statement." "3. There should no spaces in the shorthand operators. Example: + = should be +=."