Why do we use `f` for float data types, but not for bytes and short ones?
You cannot have this thing with
, for example, because you
can always handle the same thing . It is always a byte. But real numbers can only be represented as approximations. The difference between
is that it
uses 64 bits and
32. Ie The float is less important.
This is similar to
for integers. The default type for integers is
. Likewise, the default type for real numbers is
Now if you want to use precision
. You need to tell the compiler somehow. That is why it exists
. To tell the compiler how to handle the value.
Basically, when you do this
float x = 0.1f
, you are implicitly casting the literal
This statement is
float x = (float) 0.1
Now try something:
float x = 0.1f; double y = 0.1;
System.out.println(x == y)
will provide you
. What for? Let's see the first 20 digits after the decimal point:
0.1f --> 1000000014901161200 0.1 --> 1000000000000000055
Hope this helps.
source to share