__Primitive data-types in Java III - Floating points__

We continue our Primitive data-types tutorial series by discussing floating points in Java. As we said before, a floating point number is a

number that contains a fraction. Examples include 1.23, 0.35, -99.72 ....

A fractional number is called a floating point, since the point of fraction is considered "Floating", i.e., it might appear as the second character of the

number, it might appear as the 4th character of the number and so on, hence the term "floating".

number that contains a fraction. Examples include 1.23, 0.35, -99.72 ....

A fractional number is called a floating point, since the point of fraction is considered "Floating", i.e., it might appear as the second character of the

number, it might appear as the 4th character of the number and so on, hence the term "floating".

__Float & Double__

There are two primitive data-types used to represent floating points in Java, a

with the "f" character. For example, 3.101f, -1.2f . .

it can represent numbers up to a larger number of decimal places. When initializing a double, the number doesn't have to end with any

character, although the "d" character could be used. For example, 0.1234d, 0.1234, -92.23d, 0.001 are all doubles.

Please note that a double cannot be assigned to a float. For example, code below will cause a "

**Float**and a*Double*__Float:__*floating point number ( IEEE 754 floating point format ). When initializing a float, the number***A float is a 32-bit****must**endwith the "f" character. For example, 3.101f, -1.2f . .

__Double:__*floating point number ( IEEE 754 floating point format ). It is more accurate and precise than a float, since***A double is a 64-bit**it can represent numbers up to a larger number of decimal places. When initializing a double, the number doesn't have to end with any

character, although the "d" character could be used. For example, 0.1234d, 0.1234, -92.23d, 0.001 are all doubles.

Please note that a double cannot be assigned to a float. For example, code below will cause a "

*loss of precision"*error.public class FloatingPoints1 { public static void main(String[]args){ float a = 3.2; // ERROR, must be 3.2f. Will get loss of percision error float b = 0.41f; // Correct. due to the 'f' float c; double d = 0.12; //Correct, no 'd' is required double e = -41.2d; //Correct. the 'd' is optional double g = 1.2f; // Correct. Since doubles are bigger thab floats c = g; // ERROR, since g is of type double, and c is of type float g = c; // Correct. g is of type double which is larger than floats } }

__Arithmetic operations__

The arithmetic operations that could be performed on floating point numbers are the same as those that could be performed on integers.

Please observe the example below:

Please observe the example below:

public class FloatingPoints2 { public static void main(String[]args){ double a = 0.44; double b = 0.56; double c = a + b; System.out.println(c); b++; System.out.println(b); double e = 8; double f = 3; double g = e*f; System.out.println(g); double h = e/f; System.out.println(h); } }

The following is the result of running the above program:

run: 1.0 1.56 24.0 2.6666666666666665 BUILD SUCCESSFUL (total time: 1 second)

The first result "1.0" is the result of adding variables "a" and "b" with values 0.44 and 0.56 respectively. Note that the result is "1.0" and not "1".

This is because the result of adding floating point numbers is also a floating point number, therefore, the floating point stays, even if the fractional

part of the number is 0.

The number, "1.56" is the result of incrementing the variable "b". The increment/decrement operators behave similarly as when used with integers.

The result "24.0" is due to the multiplication of variables "e" and "f". Note that even though the variables were initialized with integer numbers

("8" and "3", with no floating point/ fraction), when integer numbers are saved within floating point data-types, they are automatically converted

to floating points ( a " .0 " is appended to the integer ).

The final result is due to the division of "e" by "f", with values 8 and 3 respectively. Note that unlike integer division, where the fractional part is omitted

(remember dividing " 7/3" with the result being "2"), floating point division does take the fractional part into consideration, hence, the number

"2.66666..." is generated.

In the next tutorial, we will discuss the

such as ASCII and Unicode.

This is because the result of adding floating point numbers is also a floating point number, therefore, the floating point stays, even if the fractional

part of the number is 0.

The number, "1.56" is the result of incrementing the variable "b". The increment/decrement operators behave similarly as when used with integers.

The result "24.0" is due to the multiplication of variables "e" and "f". Note that even though the variables were initialized with integer numbers

("8" and "3", with no floating point/ fraction), when integer numbers are saved within floating point data-types, they are automatically converted

to floating points ( a " .0 " is appended to the integer ).

The final result is due to the division of "e" by "f", with values 8 and 3 respectively. Note that unlike integer division, where the fractional part is omitted

(remember dividing " 7/3" with the result being "2"), floating point division does take the fractional part into consideration, hence, the number

"2.66666..." is generated.

In the next tutorial, we will discuss the

**char**data-type, and how text characters are represented in Java. We will also discuss different terms and conceptssuch as ASCII and Unicode.