3

Why I had to put f to the variable f?

float f =5.67f;

I know that double variable can accept more decimale numbers than float but why I had to add that f after 5.67 when I already said it is float variable?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Miljan Rakita
  • 1,433
  • 4
  • 23
  • 44
  • Read [Java: Why do you need to specify a 'f" in a float literal?](http://stackoverflow.com/questions/14102955/java-why-do-you-need-to-specify-a-f-in-a-float-literal/14103012#14103012) – Grijesh Chauhan Nov 07 '13 at 09:38

8 Answers8

5

when I already said it is float variable

You know, the compiler doesn't - It will try to make an implicit conversion from double to float since 5.67 is interpreted as a double by default.


When you write float f = 5.67; the compiler doesn't know that 5.67 is a float and not a double since the default type of 5.67 is a double!

float is less precise than a double so implicit conversion cannot be done and will yield an error "Type mismatch: cannot convert from double to float".

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

In Java, narrowing primitive conversions (such as double->float) can only be applied explicitly. When you write float f = 5.67; you are expecting the conversion to happen implicitly and therefore you get the error. Java defines all floating-point literals as being of the double type, regardless of the number of significant digits. The only way to specify a float literal is by appending f or F to the number.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
2

From article http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Floating-Point Literals

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.

The floating point types (float and double) can also be expressed using E or e
(for scientific    notation), F or f (32-bit float literal) and D or d (64-bit double literal; 
this is the default and by convention is omitted).

double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1  = 123.4f;

Difference Between Float And Double :

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.`

Shashi
  • 12,487
  • 17
  • 65
  • 111
0

The suffix tells the compiler how to interpret the value. The compiler will treat 5.67 as a double, as that is the default behavior. The f tells the compiler to treat it as a float instead.

phenompbg
  • 391
  • 2
  • 10
0

Because you can initialize a variable with a computation: e.g. double d = 5.4f / 2f.

dvsander
  • 95
  • 5
0

You already said f was a float, but the literal 5.67 is a double by default and you must state otherwise.

It's just like SomeObject o =new SomeObject(); both need to agree (or be able to be converted). Doubles cannot be automatically converted to floats and so you must explicitely say that 5.67 is a float not a double.

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
0

As already answered here, 5.67f is the syntax for a float literal, while 5.67 is a double literal.

Assigning a float value to a double variable requires an explicit cast (otherwise you get the "possible loss of precision" compile time error), hence you have to write either:

float f = 5.67f;

or:

float f = (float)5.67;
mkm
  • 1,545
  • 1
  • 14
  • 21
0

The value 5.67 is double-precision (Java type double).

The value 5.67f is single-precision (Java type float).

So float f = 5.67; first creates a double-precision value and then attempts to store it in a single-precision variable.

When you attempt to store a double-precision value in a single-precision variable, the compiler objects because you are (potentially, and usually) losing information.

You can use a typecast, which is what you do when the value is not a literal:

float f = (float)5.67;

but in this case it's simpler to just make the literal a float instead of a double in the first place:

float f = 5.67f;
Mark Reed
  • 91,912
  • 16
  • 138
  • 175