In Java, operations in parentheses have the highest priority. Here tip1 = 2.4 and tip2 = 0.0 although tip1 should be equal.

double mealCost = 12; int tipPercent = 20; double tip1 = mealCost*tipPercent/100; double tip2 = mealCost*(tipPercent/100); System.out.println(tip1); System.out.println(tip2); 
  • and what is the question? Why do results differ? or why exactly such results are obtained? or something else? - BOPOH
  • 2
    Well, that's right. You divide int by int and get a fractional number ..... it leads to this type and gets zero as a result of division ........ so that you don’t need to at least write tipPercent/(double)100 well or tipPercent/(float)100 ..... depending on what accuracy you want to get - Alexey Shimansky
  • @ Alexey Shimansky you wanted to write "int to int and get an integer". - Russtam
  • @Russtam no, I just put it wrong ..... it was necessary to write that as a result of dividing the data of integers (which in parentheses) a fractional number is obtained, and not to mention int by int) - Alexey Shimansky
  • @ Alexey Shimansky as a result of dividing the given integers in brackets it turns out not a fractional number, but an integer. So both times you incorrectly "expressed";) - Russtam

1 answer 1

Dividing integers gives the whole result

 double mealCost = 12; int tipPercent = 20; double tip1 = mealCost*tipPercent/100; //первый операнд double, далее все вычисляется именно в double double tip2 = mealCost*(tipPercent/100); //первый и второй операнды скобок int, деление целочисленное double tip2 = mealCost*(tipPercent/100.); //первый операнд скобок int, но второй double, поэтому деление происходит в double double tip2 = mealCost*((double)tipPercent/100); //или так System.out.println(tip1); System.out.println(tip2);