2

Why in this line the floating point number is treated as double? I thought float has enough space to hold that number.

float f = (float)333.50;
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
genek
  • 113
  • 1
  • 4
  • 11
    `333.50` is a double literal. A float literal would be `333.50f`. Basically, double is the default. – khelwood Aug 07 '17 at 13:08
  • In C and C++ the type *is* contingent on the magnitude of the literal, albeit for integral types. This is a fair question. – Bathsheba Aug 07 '17 at 13:13

6 Answers6

3

The value 333.50 can be represented in the float type but is a double literal. Use 333.50F to have a float literal.
The specification, see Oracle Java documentation about Primitive Data Types, is clear about the different literals:

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.

There is a similiar case for integer literals but here the smaller type (int) is the default:

Integer Literals

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
2

Because the Java specification says that by default a floating-point literal is a double value.

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.

Rather than using a narrowing conversion (double to float), just use the F prefix in the literal declaration :

 float f = 333.50F;
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

333.50 is a literal of type double. That's simply the rule - it comes from C way back in the 1970s. It's just like 0 is a literal of type int.

The equivalent float literal is 333.5f, or you can use a compile time evaluable constant expression like (float)333.50.

Indeed 333.5 can be represented exactly as a float as it's a dyadic rational.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

This is how you define literals in java (JLS)

int a = 12;
long b = 12L;
float c = 12.0f;
double d = 12.0d;

so you can write those like;

i = 1222; 

is an integer literal

and the same way

j = 3.1415;

is a double literal

of course you can EXPLICITLY define the type:

i = 1222L;    //for long values
j = 3.1415f;  //for float values
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

by default floating point literal is double in java , if you want the value to float you may do this

float f = 333.5f
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
0

Java by default treats any decimal point values as the double. so you need to particularly define which type of primitive data type you are using. for example:

`int i=10;         // default
long l=123456L; 
float f=123.12F;
double d=123.12345; // default

` so you must explicitly define the what type of data type you are using when not using the default types