Division between integers in Java

aneuryzm picture aneuryzm · Mar 5, 2011 · Viewed 23.2k times · Source

I need to perform division between integers in Java, and the result should be a float.

Could I just use / symbol for it? As in:

int integer1 = 1;
int integer2 = 2;
float quotient = integer1 / integer2; // Could I do this?

Answer

Adrian Cox picture Adrian Cox · Mar 5, 2011

Cast one of the integers to float to ensure a floating point division:

float result = integer1 / (float) integer2