0

^The question that my question is marked a duplicate of is not the same as this one. That question is how to get a float from dividing by an integer.

This is my code:

public class Test {
public static void main(String[] args) {
    System.out.println("33/5 is " + 33/5);
    System.out.println("33/5.0 is " + 33/5.0);
    }
}

And the output is:

33/5 is 6
33/5.0 is 6.6

Btw if this is a duplicate, please direct me to where this is answered because I was not able to find one.

TommyOKe
  • 119
  • 2
  • 5

2 Answers2

2

On the first calculation 33 / 5 you are actually dividing an integer thus giving you result with no decimal places.

And the second calculation: 33/5.0 the result is actually promoted to double which has greater precision than int and resulted to decimal.

Remember that 5.0 is a double thus promoting the result of the calculation to double primitive

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
1

int/int(33/5) will always give int so 33/5=6

int/double(33/5.0) will give double so 33/5.0=6.6

SpringLearner
  • 13,738
  • 20
  • 78
  • 116