Representation of float values ​​in Java

Take a look at the three lines of code below.

float f = 1; float g = 1.1; float h = 1.1f; 

The second line contains compilation errors, and the other lines do not have compilation errors. The first line works fine without the suffix f, and the third line works with the suffix f. Why is this?

+7
java floating-point
source share
4 answers

Floating point literals in Java are double by default.

JLS 3.10.2 floating point literals

A floating-point literal is of type float if it is a suffix with the letter ASCII F or F ; otherwise, its type is double , and it can optionally be a suffix with the letter ASCII D or D

You cannot assign a double value to float without explicitly narrowing the conversion. Therefore, you have two options:

  • For literals, use the suffix F or F to denote a float value.
  • For non-literals, use explicit listing (float)

An example of the latter:

 double d = 1.1; float f = (float) d; // compiles fine! 

About increasing conversions

The reason this compiles:

 float f = 1; 

It means that the conversion extension from int to float can be done implicitly in the destination context.

JLS 5.2 Assignment Conversion

Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow you to use one of the following:

  • extension of primitive conversion (Β§5.1.2)
  • [...]

JLS 5.1.2 Extending Primitive Conversion

The following 19 specific transformations for primitive types are called expanding primitive transformations:

  • int to long , float or double
  • [...]

Another data type suffix for literals

As mentioned above, there is also a D or D suffix for double . Consider this snippet, for example:

 static void f(int i) { System.out.println("(int)"); } static void f(double d) { System.out.println("(double)"); } //... f(1); // prints "(int)" f(1D); // prints "(double)" 

There is also a suffix for long literals, which is L or L (lowercase letter). It is highly recommended that you use the uppercase option.

JLS 3.10.1 Integer Literals

An Integer literal is of type long if it is a suffix with the letter ASCII L or L ( ell ); otherwise, it is of type int . The suffix L is preferred because the letter L ( ell ) is often difficult to distinguish from the number 1 ( one ).

+19
source share

You assign a double value to the float variable. 1.1 by itself (without f attached to the end) it is assumed that the compiler is of type double . The compiler does not like to do implicit downcasts, because there is a chance of losing accuracy.

+7
source share

The first line is autocasts int for float (ok).

The second line cannot use double for swimming due to loss of accuracy. You should use:

 float g = (float) 1.1; 

The third line does not need to be converted.

0
source share

In Java, every floating point number (any decimal point) defaults to double , which is more accurate than float . And by default, Java does not allow you to convert double to float due to loss of precision.

You can still assign double to float by casting:

 float g = (float) 1.1; 
0
source share

All Articles