If I hardcode a number in my code using the scientific notation (e.g. 1e9
) what will the type of that number be (int, long, float, double..)?
When the significand or the exponent are floating point numbers it can't be an integer obviously, but what in the above case?
N is a number, either an integer or decimal, between 1 and 10.
Scientific notation is a way to write out a number, but not a specific data type on its own. So 3.6e+3 would be the same as 3600, just different ways of writing the same thing.
The Scientific format displays a number in exponential notation, replacing part of the number with E+n, in which E (exponent) multiplies the preceding number by 10 to the nth power. For example, a 2-decimal scientific format displays 12345678901 as 1.23E+10, which is 1.23 times 10 to the 10th power.
The e
makes it a floating-point literal. From the JLS (§3.10.2. Floating-Point Literals):
A floating-point literal is of type
float
if it is suffixed with an ASCII letterF
orf
; otherwise its type isdouble
and it can optionally be suffixed with an ASCII letterD
ord
(§4.2.3).
Therefore, 1e9
of type double
, as is 1e9d
. On the other hand, 1e9f
is of type float
.
These will usually be of type double.
If you put an f
(of F
) behind it, it's a float.
double d = 1e9;
float f = 1e9f;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With