Floating Point Literals
By default every floating point literal is of double type and hence we can't assign directly to the float variable.
but we can specify floating point literal as float type by suffixed with f or F.
eg: float f = 123.456; CE: possible loss of precision fount double required float.
double d = 123.456;
float f = 123.456f;
We can specify floating point literal as double type by suffixed with d or D. Ofcourse this convention is not required.
double d = 123.456D; valid
float f = 123.456D; CE: possible loss of precision fount double required float.
double d = 123.456; valid
double d = 0123.456; valid
double d = 0x123.456; invalid CE: malformed floating point literal.
*We can specify floting point literals only in decimal form and we can't specify in octal & hexadecimal forms.
INTERVIEW FAQ:
double d = 0786; invalid
double d = 0xFace; valid
We can assign integral literal dirctly to floaring point variables& that integral literal can be specified either in Decimal , octal or Hexa decimal forms.
double d = 0786.0; // 786.0
double d = 0xFace.0; //invalid
double d = 10; //valid
double d = 0777; //valid
We can't assign floating point literals to integral types.
double d = 10;
int x = 10.0; CE: possible loss of precision found double required int.
We can specify floating point literal even in exponential form (scientific notation).
double d = 1.2e3; i.e 1.2 * 10 ^ 3
float f = 1.2e3; // CE: possible loss of precision found double required float.
float f = 1.2e3f; valid
Comments
Post a Comment