0

Why do we use f/d at the time of initialization of a float/double variable in JAVA? Eg.,

float var1 = 8.56f;
double var2 = 0.87654d;

Will that increase the speed of compilation? Or what maybe the exact reason?

Jobin
  • 5,610
  • 5
  • 38
  • 53
  • No, it's just part of a Java numeric literal syntax. There is no need to use literal qualifiers when assigning to a variable where you specified the type already. It will be needed to assign to some general variable, such as `Number o = 1.032f`. – M. Prokhorov Nov 23 '17 at 12:24
  • You should step back ... and first get clear on very fundamental things. I guess you don't mean "speed of compilation" ... but runtime performance. Which are two completely different things. – GhostCat Nov 23 '17 at 12:27
  • suppose if you declare a variable like this `float f = 3.14; // give compiler error.` Because, compiler thinks that your trying to assign the `double` 3.14 to `float` without type casting. You can do this way also, like below line without f letter. `float f = (float)3.14; //compiles fine` But in case of double it is not necessary the letter `d`. `double d = 3.14;` – Arvind Katte Nov 23 '17 at 12:29
  • Ohh that sounds great ....THANK YOU GUYS..i got my doubt clarified from your answers. – Rakesh Kumar Nov 24 '17 at 13:30

1 Answers1

1

For doubles and float, you need to specify the compiler the type of the input. Initiailizing 10.0 to a float variable would bring nothing but ambiguity to the compiler, as it would not understand if the value is float or a double.

So by specifying f/d at the end of the value, you're actually removing the ambiguity for the complier by using these format specifiers.

Safeer Ansari
  • 772
  • 4
  • 13