info@meritcampus.com    +91-85006-22255
...
`

Floating point variables conversion110
What will be the output of the following program?
`class FloatConversion{    public static void main(String args[])    {        int i = 44;        float f = 98.42f;        double d = 103.67;                f = (float) i;        d = (double) f;        i = (int) d;        System.out.println("i = " + i);        System.out.println("f = " + f);        System.out.println("d = " + d);    }}`

 A. i = 44f = 44.0d = 44.0 B. i = 103.67f = 44d = 98.42 C. Compilation error since the initialization of double d is wrong. It should be 103.67d. D. Compilation errors since we are casting integer to float and float to double.
Topic:

### User comments below.All of them might not be correct.

in the program,we have declared three variables i,f,d with int ,float and double types respectively;
f = (float)i;
in the above step, explicit casting is done and we need  not to specify (float) in the above step since explicit casting is done by the compiler based on the variables data type on the LHS.This is widening of conversion. value of f will be 44.0

d = (double)f;
Same as like above float value is explicitly casted to double and once more,widening conversion occurs from float to double.value of d wil be 44.0

i= (int)d;
Here the casting from double to int is must since a narrowing conversion occurs.And since integer cannot hold all the values that a double can hold...so compiler will throw a error if we don't cast explicitly to int...value of i will be 44;

we are printing tha values in the order i,f,d and hence the output is 44,44,0,44,0

Posted by Shashanka Mogaliraju    2014-12-08 10:20:52

Here we have the concept of type casting. Type
casting means conversion of type of variable. if the
source type is smaller than the destination type then
type conversion automatically done. if the destination type is smaller than source type then we need to
explicitly cast it.

This casting can be done between various types in
integer group, floating group and to char group.

coming to the program, Inside main are 3 variables
declared namely i of type int, f of type float , d of type
double.

f = (float) i puts value of i into f (here although not necessary, explicit casting is done). So f will become 44.0 .

d = (double) f puts value of f into d (here although not
necessary, explicit casting
is done). So d will become 44.0 .

i= (int) d puts value of d into i (here explicit casting is done). So i will become 44. Now the new values will be printed.

i = 44
f = 44.0
d = 44.0

Posted by Sai Ram    2014-12-08 10:26:33

ans is  A
typecasting is converting from one type to another type
it is of two types
1.implicit typecasting is done by compiler automatically,,no need to specify it
2.other is we deifine through an function

we can convert a smaller data type to higher preceddence data type using these type casting
so in above question it is an integer variable typecasted to float
so first 44
then next 44.0
and we again typecast which results 44.0

we used to  have precsion bits difference in float and double,,,,but output remains same here,,,,,

Posted by Sai Veerendra    2014-12-08 11:23:24

ans a there are two kinds of conversion: implicit and explicit. An implicit conversion means that a value of one type is changed to a value of another type without any special directive from the programmer. the basic rule is that implicit conversions can be done from one type to another if the range of values of the first type is a subset of the range of values of the second type. For example, a byte can be converted to a short, int, long or float; a short can be converted to an int, long, float, or double, etc. Explicit conversions are done via casting. The name of the type to which you want a value converted is given, in parentheses, in front of the value. coming to this program code casts a value of type double to a value of type int, and a value of type int to a value of type float,float to double. here initially int i=44,float f=98.42f,double d=103.67 f is assignd with 44.0 which is explicit conversion of int value.so nw f=44.0 thn double varible d is assigned with 44.0,which is explicit conversion of float value.so nw d=44.0 nxt i is assigned with 44.which is explicit conversion of double value.i=44 sop prints all these values . i=44 f=44.0 d=44.0

Posted by Maheshwari Natarajan    2014-12-08 18:24:08

This dose is now closed and the winners are Sai Ram,  for 'First Correct Comment', Sai Ram, Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Abhishek Shankhadhar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-09 03:10:02