0

I'm new to Java language and I'm preparing for the Oracle basic certification exam recently. Here's one question which make me confused while I'm looking into some sample questions.

When initiate a field in float type, you must give a value like '1.0f'. But when it goes to calculation, the 'f' is not necessary any more. Even in the result of calculation to float type fields, 'f' is missing. Just curious about what happened to it and any influence. Really appreciate if anyone could take some time to answer this, thanks in advance.

float x = 22.00f % 3.00f;
System.out.println(x);

Output after run is 1.0 instead of 1.0f

I'm using JDK1.8.

  • 5
    `f` is just a signal to the compiler that it's a float value. It's not part of the value itself, so it's not included when you print it. – shmosel Jan 06 '17 at 06:53

5 Answers5

4

f is part of a float literal, not part of the value.

2f is a float literal, but the string representation of its value is 2.0. f is just there to tell the compiler that "this is a float literal!"

In fact, if you just do:

float x = 22.00f;
System.out.println(x);

You get the output 22.0. You still lose the f.

It's just like string literals. When you write them in code, you add ""s:

String a = "Hello";

But when you print them out, you lose the ""s.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Because the documentation says so. System.out is a PrintStream, so:

PrintStream.println(float x): This method behaves as though it invokes print(float) and then println().

PrintStream.print(float f): The string produced by String.valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

String.valueOf(float f): The representation is exactly the one returned by the Float.toString method of one argument.

Float.toString(float f): If m is greater than or equal to 10-3 but less than 107, then it is represented as the integer part of m, in decimal form with no leading zeroes, followed by '.' ('\u002E'), followed by one or more decimal digits representing the fractional part of m.

No "float" suffix is added.

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

The "f" character is simply a float type suffix in float literals as explained in JLS §3.10.2. Floating-Point Literals. Literals are used in source code only. The compiler has to interpret them as a specific type.

Especialy, it is not part of the value that is stored in a float typed variable. Look at this code snippet:

float a = 22.00f;
float b = 3.00f;
float x = a % b;
System.out.println(a);
System.out.println(b);
System.out.println(x);

This prints out:

22.0
3.0
1.0

Something completely different: Are you aware that you are using the modulo operation % which behaves as an integer-only operation when using with floats? Float values are truncated (not rounded) before actually being divided. See JLS §15.17.3. Remainder Operator % for more information.

Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • 1
    Your last remark can be interpreted in multiple ways. In Java, the **remainder** operator `%` can be invoked with floats and doubles. The result will also be a float or double, but will not have a fractional part. (From the JLS: "In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.)" – Erwin Bolwidt Jan 06 '17 at 07:20
  • @ErwinBolwidt I reworded that sentence. Maybe the point gets more clear now. – Seelenvirtuose Jan 06 '17 at 07:48
0

From my understanding, the main reason to add 'f' after a number is to distingush it from double. For example

float x= 2.002f;
double y = 2.002;

'f' just tell java that the number is a float. When you run a calculation, 'f' have no effect on it.

george970
  • 31
  • 4
0

Please don't see this as a blind float value float f=22.00f;

This is to intimate the compiler that the floating value 22.00 is of type float. By default 22.00 will be taken as double, suffix f is to intimate the compiler that its float and not double.