-2

I was reading someone's java code and came across this declaration:

private float x = 34f, y = 34f;

What does the f stand for? I have never seen this type of declaration before. I can easily assume that it stands for "float" since the type is float, but that's just a wild guess.

Any explanation would be helpful, thanks in advance.

almost a beginner
  • 1,622
  • 2
  • 20
  • 41

3 Answers3

4

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.

f at the end of 34 makes it a float literal instead of a int literal

Sajith Eshan
  • 696
  • 4
  • 17
  • 1
    34 with f or F at the end is interpreted as a floating point literal. – Sajith Eshan Jun 09 '15 at 08:58
  • 1
    Please get your types and the casts straight. `34.4` is a `double` literal (`float f = 34.4;` will result in a compiler error). `34.4f` is a `float` literal. If you write `float f = 34;`, it will compile, but `34` is in fact an `int` literal and gets upcasted. – Turing85 Jun 09 '15 at 09:03
  • 1
    [Link to quoted section of Java Language Specification](http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.2). – VGR Jun 09 '15 at 09:19
1

The f means it's a float literal. Without it, 34 would be an int literal that the compiler would have to cast to a float.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

It tells the compiler is x and y double or float value.

Jon Koivula
  • 899
  • 1
  • 11
  • 24