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

JavaFloating PointDouble

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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)

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

It defines the datatype for the constants 5 and 3.0.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBen C.View Question on Stackoverflow
Solution 1 - JavaEboMikeView Answer on Stackoverflow
Solution 2 - JavaJason RogersView Answer on Stackoverflow
Solution 3 - JavaJeffView Answer on Stackoverflow
Solution 4 - JavaRonnie HowellView Answer on Stackoverflow
Solution 5 - JavaTheresa ForsterView Answer on Stackoverflow
Solution 6 - JavaJoshua MartellView Answer on Stackoverflow