8

Here's the explanatory code. The language is Java and the code uses Android.

fg.setTextSize(height*0.50f); //<-'f' is in the brackets

or

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    sWidth = w / 3f; // <-'f' is here
}

What does the suffix 'f' mean?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
sandalone
  • 41,141
  • 63
  • 222
  • 338
  • Possible duplicate of [What do F and D mean at the end of numeric literals?](http://stackoverflow.com/questions/4331200/what-do-f-and-d-mean-at-the-end-of-numeric-literals) – Ivar Apr 19 '17 at 13:01

5 Answers5

13

It indicates a float literal.

Phil Hunt
  • 8,404
  • 1
  • 30
  • 25
  • Hard to understand for a guy coming from .NET :). Anyway, is it like casting? Is the same 3f and (float)3? What would happen if I omit it? – sandalone Jan 11 '11 at 15:56
  • 1
    Then it would be an integer. L is used for long, d for double – Kennet Jan 11 '11 at 16:05
  • 3
    Dropping the f from w/3f will cause integer division to occur, rather than floating point division because 3 is an int while 3f is a float. Both 3f and ((float) 3) will evaluate to the same value, but (float) 3 may be converted at runtime, while 3f is always evaluated at compile time. – ILMTitan Jan 11 '11 at 16:07
  • 2
    @askmo, the same thing works in .net. EG if you want to declare a decimal literal, you can write it as `5m`, or look at the float example at http://msdn.microsoft.com/en-us/library/b1e65aza(v=VS.100).aspx – dsolimano Jan 11 '11 at 16:09
  • @askmo: I'm not sure why this is so hard to understand, C# also uses f to represent float values, according to the [Value Types Table](http://msdn.microsoft.com/en-us/library/bfft1t3c.aspx). – Powerlord Jan 11 '11 at 16:11
  • 1
    Thanks guys. It wasn't hard to understand. I just haven't ever worked in such way in C#. Now it's clear. Thanks again. – sandalone Jan 11 '11 at 16:54
  • 1
    Apparently I was wrong, ((float) 3) is a compile time constant, and will be evaluated at compile time. – ILMTitan Jan 11 '11 at 18:53
  • "3" is an integer, "3f" is a float, "3d" is a double. Sometimes the distinction is important. Sometimes javac is picky, e.g. "public static float FOO = 3.0" results in a "possible loss of precision" error because the values are assumed to be double by default. (FWIW, "FOO = 3" works fine.) – fadden Jan 12 '11 at 00:54
5

It indicates 3 is float not integer in other case 0.50 is float not double. Just like in any other java program.

Tasawer Khan
  • 5,994
  • 7
  • 46
  • 69
2

float ;)

it's a 3 that is a float, not an int

Nanne
  • 64,065
  • 16
  • 119
  • 163
2

It's a float, not a double. This is basic Java (C) notation.

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
1

You don't need to append the f if entering 0 or 1 or during certain assignments. But other times you do need it especially when the compiler can't tell if its a float or a double or an int or needs to cast the value in some way. It's maddeningly inconsistent really.

locka
  • 5,809
  • 3
  • 33
  • 38