Unexpected value

We consider:

double result = 3 / 5;

Why does our variable result shows a value of 0.0 ?

Solution

3 and 5 are both literals of type int. According to Figure 119, “Binary operator type examples ” the operator / acting on two int values evaluates to an int. The resulting value denotes the number of times 5 fits into 3 being equal to 0 of type int. This value of 0 is being assigned to the variable result of type double using widening resulting in 0.0.