float r = 5.0f
or
double r = 5.0
The value 5.0 is a number represented as a decimal fraction (has an integer and a fractional part). In computer science, such numbers are called
real
Real number is a number that has an integer part and a fractional part. The integer and fractional parts
are separated from each other by a dot, not by a comma as in mathematics.
Even if the fractional part of the number is zero, as in the
\(r\) variable in the example, the translator will still create a real variable in memory. The point, as it were, a signal for the translator that it is necessary to create a real variable.
Very large and very small numbers are written using "floating point" (in the so-called
scientific format).
In scientific format, a number is represented as
mantissa(significant part of the number) and
exponent. When notated, the mantissa and exponent are separated from each other by the letter e (denoting 10 to some extent).
For example, you can store the value of the charge of an electron (
\(1.60217662 \times 10^{-19}\) C) in a variable, writing in the following form
float El = 1.60217662e-19f //for a positive order, the + sign can be omitted
or
doubleEl= 1.60217662e-19
Almost all real numbers cannot be stored in computer memory with perfect accuracy, since a limited number of bits are allocated for their storage. Therefore, when calculating with real numbers, errors associated with the inaccuracy of the representation accumulate. Moreover, the less space allocated, the greater this error will be. In order to reduce the error in Java, the
double
type is used, which stores a real number with double precision in memory (occupies eight bytes in memory, while the
type \(float \)- 4 bytes)