Watch where the cast applies
System.out.println(9 / 2); // 4 (both int)
System.out.println(9 / 2.0); // 4.5 (one double)
System.out.println((double)9 / 2); // 4.5 (9 becomes 9.0 first)
System.out.println((double)(9 / 2)); // 4.0 (int 9/2=4 first, then cast)
The last one is the trap: the division happens before the cast, so the fractional part is already lost.