A few related questions here.
As per the title, why is it a requirement if we are specifying the variable type as long or float, double? Doesn't the compiler evaluate the variable's type at compile time?
Java considers all integral literals as int
- is this to lessen the blow of inadvertent memory waste? And all floating-point literals as double
- to ensure highest precision?
Every floating point literal expression without the qualifying F is of type double, even if it would fit into a float. You should use the L even after small values, eg 123L, so the compiler puts them into 64 bits rather than its having to undergo a widening conversion later.
In programming, A floating point value for example, 2.40 is considered as double by default. hence we have to use an f to mention explicitly that the number is float.
long represent Int64 type i.e an integral number while float represents Single type i.e a floating point number. And even though the size of long is larger than that of float, it is not possible to convert from a float to an integer without loosing information. For more information on long and float type refer msdn.
When representing a float data type in Java, we should append the letter f to the end of the data type; otherwise it will save as double. The default value of a float in Java is 0.0f. Float data type is used when you want to save memory and when calculations don't require more than 6 or 7 digits of precision.
When you have a constant there are subtle differences between value which look the same, but are not. Additionally, since autoboxing was introduce, you get a very different result as less.
Consider what you get if you multiply 0.1 by 0.1 as a float or as a double and convert to a float.
float a = (float) (0.1 * 0.1);
float b = 0.1f * 0.1f;
System.out.println("a= "+new BigDecimal(a));
System.out.println("b= "+new BigDecimal(b));
System.out.println("a == b is " + (a == b));
prints
a= 0.00999999977648258209228515625
b= 0.010000000707805156707763671875
a == b is false
Now compare what you get if you use either float
or int
to perform a calculation.
float a = 33333333f - 11111111f;
float b = 33333333 - 11111111;
System.out.println("a= "+new BigDecimal(a));
System.out.println("b= "+new BigDecimal(b));
System.out.println("a == b is " + (a == b));
prints
a= 22222220
b= 22222222
a == b is false
Compare int
and long
long a = 33333333 * 11111111; // overflows
long b = 33333333L * 11111111L;
System.out.println("a= "+new BigDecimal(a));
System.out.println("b= "+new BigDecimal(b));
System.out.println("a == b is " + (a == b));
prints
a= -1846840301
b= 370370362962963
a == b is false
compare double
with long
double a = 333333333333333333L / 333333333L;
double b = 333333333333333333D / 333333333D;
System.out.println("a= "+new BigDecimal(a));
System.out.println("b= "+new BigDecimal(b));
System.out.println("a == b is " + (a == b));
prints
a= 1000000001
b= 1000000000.99999988079071044921875
a == b is false
In summary its possible to construct a situation where using int
, long
, double
or float
will produce a different result compared with using another type.
This becomes important when you do more than a simple assignment. If you take
float x = 0.1 * 3.0;
it makes a difference if the computer does the multiplication in double precision and then converts to single precision or if it converts the numbers to single precision first and then multiplies.
edit: Not in this explicit case 0.1 and 3.0, but if your numbers become complex enough, you will run into precision issues that show differences between float and double. Making it explicit to the compiler if they are supposed to be doubles or float avoids ambiguity.
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