Question

I was interested to see a weird error when performing the following:

int width = 300;
int var1 = width/3;
int var2 = width * 1/3;
int var3 = width * 2/3;
int var4 = width*(1/3);
int var5 = width * (int)(1/3);
int var6 = (int)(width*(1/3));

System.out.println("Var1: " + var1);
System.out.println("Var2: " + var2);
System.out.println("Var3: " + var3);
System.out.println("Var4: " + var4);
System.out.println("Var5: " + var5);
System.out.println("Var6: " + var6);

Output is this:

Var1: 100
Var2: 100
Var3: 200
Var4: 0
Var5: 0
Var6: 0

Why do the last three operations return 0? The parenthesis around the 1/3 seems to change something with how it performs the calculation, but even trying to case it into an int did not change the output.

I'm using the BlueJ IDE and Java version 1.7.0_17


EDIT

I found the answer. It's due to the order of operation. The parens cause the division to occur first, and with an int 1/3 or 2/3 will always equal 0. If I allow the width * 2 to occur before dividing by 3 everything is OK because the number is larger than 1.

Sometimes it's just basic math... :)

Was it helpful?

Solution 2

It's relevant, so see Order of Precedence as well.

OTHER TIPS

For the fourth case try this, notice that by adding a decimal point we're forcing a decimal division, not an integer division as it was before:

int var4 = width*(1/3.0);

For the last two cases, try this instead - although this ends up being equivalent to other cases before!

int var5 = width * (1/3);
int var6 = (width*(1/3));

Notice that (int)1/3 is 0, the cast is making the division return 0, and 0 times anything will return ... well, 0. There's no way those two cases are going to work if you leave the cast in place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top