Why this result is alway 0 ?

Hello, In my java code, I have:int test1=4;int test=7;int div=test1/test;System.out.println("div:"+div);But I always get div value is 0, why?
[190 byte] By [Mellona] at [2007-11-27 3:16:07]
# 1
What value were you expecting?
DrLaszloJamfa at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...
# 2
Sorry, I mistake the div type is double not int , the result still 0. why
Mellona at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

Aknibbsa at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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

Aknibbsa at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...
# 5
Hi, it is my hand typing mistake. my code is like this: int test1=4;int test=7;double div=test1/test;System.out.println("div:"+div);the result is 0. why?
Mellona at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...
# 6
hi,Aknibbs , you are right. when chage test1 and test2 to double type, the result comes. thanks.
Mellona at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...
# 7

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".

JJCoolBa at 2007-7-12 8:18:41 > top of Java-index,Java Essentials,Java Programming...