0

Someone please explain me how the below code prints 8.800000095367432.

class Test
{
public static void main(String[] args)
{
    Double object = new Double("2.4");
    int a = object.intValue();
    byte b = object.byteValue();
    float d = object.floatValue();
    double c = object.doubleValue();

    System.out.println(a + b + c + d );

}
}

Dear Pavel,The question in another link regards to javascript. I was wondering how is it in Java.

To make it more clear, The below one prints 6.4

 Double object = new Double("2.4");
        int a = object.intValue();
        byte b = object.byteValue();
        double c = object.doubleValue();

        System.out.println( a+c+b);

Where as,

Double object = new Double("2.4");
    int a = object.intValue();
    byte b = object.byteValue();
    double c = object.doubleValue();
    float d = object.floatValue();
    System.out.println(a + c + b + d);

prints 8.800000095367432.

BSM
  • 173
  • 3
  • 13
  • it is printing 222.42.4 for me... – codingenious Nov 22 '17 at 06:49
  • They don't get 'converted to common type'. They get *widened* to the widest type used in the expression, which is `double`. – user207421 Nov 22 '17 at 06:54
  • if comment the doubleValue() line in the above snippet I still get the same o/p. I am thinking it is because of float data type. But I am unsure what exactly happening with float type. – BSM Nov 22 '17 at 06:58

0 Answers0