Without using Eclipse, what is the output produced by the following lines of program code? (Trace the result using the State Diagram) Double result = (1/2)* 2: System.out.println(“(1/2)* 2 equals” result);
Expert Answer
Answer: I am assuming Double is a primitive data type it means double. If it is a primitive data type in this case then output should be below
(1/2) * 2 equals 0.0
Below first (1/2) value calculate. So it will produce 0 instead of 0.5 because both values are integers here.
Case 2
If Double is wrapper class then there is compile error. We should convert the value from int to double.. So in this case code should like below
Double result = (double)(1/2) * 2;
System.out.println(“(1/2) * 2 equals “+result);
In this case also, same output will be printed.
(1/2) * 2 equals 0.0