-3
public class TypeConversion4 {

    public static void main(String[] args) {
        double d = 2D + 2d + 2. + 2l + 2L + 2f + 2F + 2.f + 2.D;
        System.out.println(d); //prints 18.0
    }
}

how it prints 18.0. Can anyone provide some analysis.

kittu
  • 6,662
  • 21
  • 91
  • 185

1 Answers1

1

So, what's the problem? All these twos converted to the biggest type while summing up and then the result casted to double. But you can store 2 in long, int, double and float without any error. That means that all you have to do is to sum these 9 twos and come up with 18.0.

antonpp
  • 2,333
  • 23
  • 28