> Hello, In my java code, I have:
>
> int test1=4;
>int test=7;
>int div=test1/test;
>System.out.println("div:"+div);
> t I always get div value is 0, why?
try changing your values from int to double.
Then get back to us.
> Sorry, I mistake the div type is double
> not int , the result still 0. why
look at it like this
I have a slot to store a double called div:
I have an int (4) and i'm dividing it by an int (7)
int/int so you get an int result (0)
an integer can fit in a double so div = 0
Even though you're storing your answer into a double, the numbers you are dividing are both integers. That means your result is going to be an integer shoved into a memory spot for a double. It works fine, but it doesn't change the fact that you're dividing integers to get an integer result. Integers DO NOT STORE DECIMALS. So when you put 4/7 into a calculator, you get 0.5714 .... which, being an integer and not storing decimals, comes out to be "0".