Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasoning behind having to specify L for long, F,D for float, double

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?

like image 451
wulfgarpro Avatar asked Sep 23 '11 00:09

wulfgarpro


People also ask

Why do we put L after a long java?

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.

Why do you need f after float?

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.

Why is long a subtype of 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.

Why do we use F after float in java?

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.


2 Answers

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.

like image 88
Peter Lawrey Avatar answered Sep 27 '22 17:09

Peter Lawrey


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.

like image 33
Stefan Werner Avatar answered Sep 27 '22 18:09

Stefan Werner