float f = 1;
float g = 1.1;
float h = 1.1f;
second line has compilation error. While rest of line do not have compilation error.Please suggest.
First line is working fine without suffix f and third line is working with suffix f.
float f = 1;
float g = 1.1;
float h = 1.1f;
second line has compilation error. While rest of line do not have compilation error.Please suggest.
First line is working fine without suffix f and third line is working with suffix f.
You're assigning a double
value to a float
variable. 1.1
by itself (without the f
tacked on the end) is assumed by the compiler to be of type double
. The compiler doesn't like to make implicit downcasts because there's potential there to lose precision.
First line autocasts int to float (ok).
Second line could not cast double to float because of lost of precision. You have to cast:
float g = (float) 1.1;
Third line does not need to convert.
In Java, every floating-point number (any number with a decimal point) defaults to a double
, which is more precise than a float
. And by default, Java does not let you convert a double
to a float
because of the loss of precision.
You can still assign a double
to a float
by casting:
float g = (float) 1.1;
Either your declare g
as double or suffix 1.1
with f
as you did for h
.from documentation ,
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).
Floating point literals in Java is a double
value by default.
JLS 3.10.2 Floating-Point Literals
A floating-point literal is of type
float
if it is suffixed with an ASCII letterF
orf
; otherwise its type isdouble
and it can optionally be suffixed with an ASCII letterD
ord
.
You can't assign a double
value to a float
without an explicit narrowing conversion. You therefore have two options:
f
or F
to denote a float
value(float)
An example of the latter is:
double d = 1.1;
float f = (float) d; // compiles fine!
The reason why this compiles:
float f = 1;
is because the widening conversion from int
to float
can be done implicitly in the context of an assignment.
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 the use of one of the following:
- a widening primitive conversion (§5.1.2)
- [...]
JLS 5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening primitive conversions:
int
tolong
,float
, ordouble
- [...]
As mentioned above, there's also the 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's also a suffix for long
literals, which is L
or l
(lowercase letter). It is highly recommended that you use the uppercase variant.
JLS 3.10.1 Integer Literals
An integer literal is of type
long
if it is suffixed with an ASCII letterL
orl
(ell
); otherwise it is of typeint
. The suffixL
is preferred, because the letterl
(ell
) is often hard to distinguish from the digit1
(one
).