Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 405
Ben C. Avatar asked Dec 02 '10 02:12

Ben C.


People also ask

What does D and F mean in 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.

What is F at the end of a number?

The "F" indicates that the literal numeric value it is appended to is a float value.

What does F mean in float?

f = float. In c a value of 1 is an integer and 1.0 is a double, you use f after a decimal number to indicate that the compiler should treat it as a single precision floating point number. e.g.: If you have a line.

What does D mean in Java?

The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character.


2 Answers

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.

like image 57
EboMike Avatar answered Oct 03 '22 06:10

EboMike


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

like image 40
Jason Rogers Avatar answered Oct 03 '22 08:10

Jason Rogers