172k views
3 votes
Double x = 4.0;

System.out.println(x * 3 / 2);
System.out.println(x * (3 / 2));
Will the two print statements print the same thing? Explain why
or why not.

User Mark Szabo
by
7.7k points

1 Answer

6 votes

Final answer:

No, the two print statements will not print the same thing.

Step-by-step explanation:

No, the two print statements will not print the same thing.

In the first print statement, x * 3 / 2, the multiplication operation (x * 3) is performed first, resulting in 12.0. Then, the division operation (12.0 / 2) is performed, resulting in 6.0.

In the second print statement, x * (3 / 2), the division operation (3 / 2) is performed first, resulting in 1.5. Then, the multiplication operation (x * 1.5) is performed, resulting in the value of x (4.0) multiplied by 1.5, which is 6.0. Therefore, the second print statement will also print 6.0.

User Zheng Qsin
by
8.2k points