0

So it is obvious that

 int x = 3/4; // x is 0 here

But i do not understand why the following is also the case:

double x = 3/4; // x is also 0 here

With the following i am able to get the result i want:

 double x = (double) 3/4; // x is 0.75 here, but why do i have to cast?
M.Dietz
  • 900
  • 10
  • 29
  • 3
    `double x = 3/4; ` is still integer division. the receiver type doesn't change anything in the computation. – Ousmane D. Jun 10 '18 at 16:29
  • Actually java promotes Integer for integer type of computations. That's why you need to cast. – Gaurav Srivastav Jun 10 '18 at 16:31
  • `3/4` is integer (integer divided by integer). Assigning it to a double doesn't change that fact. That's why 3/4 is always 0. However `(double) 3/4` is double (double divided by integer). – Ahmad Shahwan Jun 10 '18 at 16:39

0 Answers0