59

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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Ben C.
  • 1,148
  • 3
  • 13
  • 17
  • 1
    For those coming from C: 1) `d` suffix does not exist in ANSI C, only as a GNU extension. 2) `1f` is not possible in C, you must use `1.0f`. 3) For hex integer literls, `d` and `f` don't work as they would be ambiguous with the number itself, e.g. `0x1f` is `31`, not `1.0f` – Ciro Santilli OurBigBook.com Feb 23 '15 at 10:20
  • Note that in C with GCC, even `5D` is invalid as being an integer constant; `5.D` would be OK. ICC 15 silently regards such numbers as 0. And with tcc 0.9.27, one gets a compile-time error. – vinc17 Jun 19 '18 at 23:29

6 Answers6

60

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.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 2
    with this, is 5.0 == 5d? – Ungeheuer Dec 04 '16 at 22:33
  • 1
    @Ungeheuer Yes. "A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d." https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html – anishpatel Sep 23 '19 at 17:54
22

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

jwd
  • 10,837
  • 3
  • 43
  • 67
Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
6

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)

Jeff
  • 21,744
  • 6
  • 51
  • 55
2

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.

Ronnie Howell
  • 639
  • 3
  • 8
1

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

Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
  • 1
    *"autobox"* is the wrong terminology for what you describe. What you mean is the opposite of promotion, which would be demotion, but there are no (automatic) rules for that. – Tom Oct 22 '16 at 13:06
  • I just did the Oracle java SE8 Java Programmer course, its right here in the notes, defaults and the fact that autoboxing/unboxing does not occur with reduction of precision and so type definition is required or casting for reduction of precision. as the type definition – Theresa Forster Oct 22 '16 at 13:13
  • [Autoboxing/unboxing](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html) occurs when one (or the compiler) "converts" a primitive type to its corresponding wrapper type `int` -> `Integer`. What you're talking about is the opposite of [promotion](http://stackoverflow.com/questions/1660856/promotion-in-java) and yes, there is no automatic conversion due to the lost of precision. – Tom Oct 22 '16 at 13:17
-2

It defines the datatype for the constants 5 and 3.0.

Joshua Martell
  • 7,074
  • 2
  • 30
  • 37