What do F and D mean at the end of numeric literals? - java

I've seen some of this symbols, but I cannot find anything strange with it,
double d = 5D;
float f = 3.0F;
What does the D and F behind 5 exactly means?

Means that these numbers are doubles and floats, respectively. Assume you have
void foo(int x);
void foo(float x);
void foo(double x);
and then you call
foo(5)
the compiler might be stumped. That's why you can say 5, 5f, or 5.0 to specify the type.

D stands for double
F for float
you can read up on the basic primitive types of java here
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
I would like to point out that writing
5.1D or 5.1 : if you don't specify a type letter for a comma number then by default it is double
5 : without the period, by default it is an int

They're format specifiers for float and double literals. When you write 1.0, it's ambiguous as to whether you intend the literal to be a float or double. By writing 1.0f, you're telling Java that you intend the literal to be a float, while using 1.0d specifies that it should be a double. There's also L, which represents long (e.g., 1L is a long 1, as opposed to an int 1)

D stands for double and F stands for float. You will occasionally need to add these modifiers, as 5 is considered an integer in this case, and 3.0 is a double.

As others have mentioned they are the Type definitions, however you will less likely see i or d mentioned as these are the defaults.
float myfloat = 0.5;
will error as the 0.5 is a double as default and you cannot autobox down from double to float (64 -> 32 bits) but
double mydouble = 0.5;
will have no problem

It defines the datatype for the constants 5 and 3.0.

Related

Literal Assignment in Java [duplicate]

This question already has answers here:
Java's L number (long) specification
(7 answers)
Closed 6 years ago.
what's the difference in defining
double example = 23.1d
or
double example = 23.1
Why long, float and double can end with l, f, d?
There is no difference between double example = 23.1d; and double example = 23.1; because a floating point literal without a type suffix is always interpreted as a double.
The type suffixes are necessary in order to avoid ambiguities in certain scenarios.
For example, java supports method overloading. This means that you can have void x( float f ); and void x( double d ); Both methods are called x; which one will be selected depends on the type that you pass; if you pass a variable which is already known to be either float or double, things are clear; but if you want to pass a literal, like this: x( 5 ); then you have to be able to specify whether you mean this 5 to be a float or a double, so as to select the right method.
There are a few other very nuanced situations where the type of the literal matters. For example, the following code:
System.out.println( "" + (2/3.3333) );
System.out.println( "" + (2/3.3333f) );
Yields the following output:
0.6000060000600006
0.600006
...because the first number is a double, while the second number is a float.
Similar disambiguation concerns make the "L" type suffix necessary for long integer literals.
23.1d (or just 23.1, since double is the default) is a different number than 23.1f. Since 23.1 cannot be exactly represented, 23.1f is the closest float to 23.1, and has only about 6 significant figures. As a double, 23.1 will have about 16 significant figures and can therefore get a bit closer to the actual value.
Note that
double example = 23.1f;
is equivalent to
float f = 23.1f;
double example = (double)f;

Math.ceil(x / 50d) - What is the "50d"

I jsut found this code snippet and was wondering what the 50d actually means? I've been searching the web, but can't find anything useful.
Here' the complete code snippet:
public int CalculateMinimumDiameterFilter(){
CalculateFilterAreaRequirement();
double val1 = filterAreaRequirement /Math.PI;
double val2 = Math.sqrt(val1);
double val3 = val2 * 2;
int val4 = (int)Math.round(val3 * 1000);
minimumDiameterFilter = (int) (Math.ceil(val4 / 50d) * 50);
return minimumDiameterFilter;
}
Can anyone explain to me what the 50d actually does?
It is an explicit declaration of a number to be a double as defined in the JLS
3.10.2. Floating-Point Literals
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d
You may take a look at Java Primitive Data Types which will give you a good explanation.
50d in this case means the 50 is not of type integer (int) by of type double (50.0).
Not only we can do 50d. You may also see people writing things like:
long serialNo = 1234567L;
In this case the L after the number means that I want to let serialNo be of type long. If you don't place the L, the default type of whole number will be integer (int).
Instead of writing 50d, the person who writes that code could have written it as 50.0 as well. It will mean the same thing in this case because by default, floating point values in Java are double. Whole numbers are int unless otherwise specified by the postfix (such as l, d, f..etc).
Example:
double d1 = 50.0; //ok! 50.0 will be double by default
double d2 = 50.0d; //ok! 50.0 is already in double, no harm putting a "d"
double d3 = 50d; //ok! 50 becomes a double
double d4 = 50; //ok! trying to store int into double (no loss of precision)
double d5 = 50f; //ok! trying to store float into double (no loss of precision)
float f1 = 0.1f; //ok!
float f2 = 0.0; //not ok! trying to store double into float (loss of precision)
float f3 = 50d; //not ok! trying to store double into float (loss of precision)
50d means the number 50 with double type.
This is required because val4 as an int, divided by 50 as an int, will return an int and Java will be rounding it to the floor, which is not what is expected.
d is short for double. Standard the number 50 without any decimal (ie: 50.0) is just an int. You need to add the d to plain 50 to state that you want it expressed as a double.
The d suffix forces it to be a double literal instead of an int literal.

Loss of precision in java

All integer literals are treated as int in java and floating point literals are treated as double in java.
Then why does
byte b =10;
does not give any error but
float f = 10.0;
gives a loss of precision error when in both cases down-casting takes place?
In the case of int to byte, there's no real concern about a loss of precision, because both types have the same degree of granularity. You'll get an error if you try to convert a literal with a value outside the range of byte to byte. (The error message given in that case is slightly misleading.)
In the case of double to float, you can have a constant value which is in the right range, but still lose precision. In your specific case of 10.0, the value can be represented exactly in both float and double, but that's not the case in general.
As an example of that, consider this:
float f = (float) 10.1; // Or float f = 10.1f;
double d = 10.1;
System.out.println(f == d); // Prints false
That's because precision is being lost in the conversion from double tofloat - neither type can represent 10.1 exactly, but double gets close to it than float does. The == operator will mean f is converted back to a double, with a different value to d.

information loss from long to float in Java [duplicate]

This question already has answers here:
Why does Java implicitly (without cast) convert a `long` to a `float`?
(4 answers)
Closed 8 years ago.
if you call the following method of Java
void processIt(long a) {
float b = a; /*do I have loss here*/
}
do I have information loss when I assign the long variable to the float variable?
The Java language Specification says that the float type is a supertype of long.
Do I have information loss when I assign the long variable to the float variable?
Potentially, yes. That should be fairly clear from the fact that long has 64 bits of information, whereas float has only 32.
More specifically, as float values get bigger, the gap between successive values becomes more than 1 - whereas with long, the gap between successive values is always 1.
As an example:
long x = 100000000L;
float f1 = (float) x;
float f2 = (float) (x + 1);
System.out.println(f1 == f2); // true
In other words, two different long values have the same nearest representation in float.
This isn't just true of float though - it can happen with double too. In that case the numbers have to be bigger (as double has more precision) but it's still potentially lossy.
Again, it's reasonably easy to see that it has to be lossy - even though both long and double are represented in 64 bits, there are obviously double values which can't be represented as long values (trivially, 0.5 is one such) which means there must be some long values which aren't exactly representable as double values.
Yes, this is possible: if only for the reason that float has too few (typically 6-7) significant digits to deal with all possible numbers that long can represent (19 significant digits). This is in part due to the fact that float has only 32 bits of storage, and long has 64 (the other part is float's storage format † ). As per the JLS:
A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
By example:
long i = 1000000001; // 10 significant digits
float f = i;
System.out.printf(" %d %n %.1f", i, f);
This prints (with the difference highlighted):
1000000001
1000000000.0
~ ← lost the number 1
It is worth noting this is also the case with int to float and long to double (as per that quote). In fact the only integer → floating point conversion that won't lose precision is int to double.
~~~~~~
† I say in part as this is also true for int widening to float which can also lose precision, despite both int and float having 32-bits. The same sample above but with int i has the same result as printed. This is unsurprising once you consider the way that float is structured; it uses some of the 32-bits to store the mantissa, or significand, so cannot represent all integer numbers in the same range as that of int.
Yes you will, for example...
public static void main(String[] args) {
long g = 2;
g <<= 48;
g++;
System.out.println(g);
float f = (float) g;
System.out.println(f);
long a = (long) f;
System.out.println(a);
}
... prints...
562949953421313
5.6294995E14
562949953421312

What is the purpose of having a suffix for Java Primitive Data Types?

Sorry for a beginners question! I am learning Java and I'm a bit confused on one topic.
For example, I am declaring and initializing a variable as a float such as:
float myVariable = 9.5;
In some tutorials online, I see floats being declared and initalized like that, but also with a suffix such as this:
float myVariable = 9.5F;
What is the purpose of the the suffix if I am already saying "float myVariable"?
Thanks!
Floating point literals are by default of double type. So, 9.5 is double not float. To make it float we append an f at the end.
So, in first case:
float myVariable = 9.5;
You are trying to assign a double type literal to a float type variable, which requires an explicit typecasting. The statement won't compile. You need to add a cast there:
float myVariable = (float) 9.5;
While in 2nd case:
float myVariable = 9.5F;
there is no conversion.
Java uses suffixes with these primitives types, here is a brief intro:
float
float fVariable = 5.5 is by default double while float fVariable = 5.5f makes it float
long
long lVariable = 55 here literal 55 is by default int type but long lVariable = 55l makes it long type.
double
double dVariable = 5.5 and double dVariable = 5.5d are similar but here d suffix is optional
Have you tried to compile your code?
This will not compile, because by default all floating-point literals are doubles
float myVariable = 9.5;
You can use explicit cast operation
float myVariable = (float) 9.5;
But it's too verbose, so thankfully the 'F' suffix was introduced:
float myVariable = 9.5F;
BTW, both UPPER ('F') and lower ('f') cases are allowed.
Thought it might be worth providing an answer that cites the specification. From §3.10.2 Floating-Point Literals:
A floating-point literal is of type float if it is suffixed with an
ASCII letter F or f; otherwise its type is double...
In other words, 9.5 and 9.5f are two distinct literals with different types.
9.5 is of type double and cannot be assigned to a float without
explicit casting
9.5f is of type float (and as such can obviously be
assigned to a variable of type float)

Categories

Resources