Ben C.
Ben C.

Reputation: 1178

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

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?

Upvotes: 60

Views: 76062

Answers (6)

Jason Rogers
Jason Rogers

Reputation: 19344

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

Upvotes: 22

Theresa Forster
Theresa Forster

Reputation: 1932

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

Upvotes: 1

Jeff
Jeff

Reputation: 21892

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)

Upvotes: 6

Ronnie Howell
Ronnie Howell

Reputation: 639

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.

Upvotes: 2

EboMike
EboMike

Reputation: 77742

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.

Upvotes: 60

Joshua Martell
Joshua Martell

Reputation: 7212

It defines the datatype for the constants 5 and 3.0.

Upvotes: -2

Related Questions