1

Possible Duplicate:
Why the result of 1/3=0 in java?

I'm working with java, and part of my code requires a number to be divided by 10. To simplify (and show my question) I just put it into a println:

System.out.println(1/10);

that prints out 0. So, logically, I figured it was casting it to an int, so I tried

System.out.println((double)1/10);

and that printed out the proper 0.1. I don't understand why it automatically cast it into an int the first time though. Where else does it do this? Why?

Community
  • 1
  • 1
r0nk
  • 713
  • 2
  • 6
  • 7
  • 2
    Try System.out.println(1.0/10). If both the values are int the result is automatically casted to int. However if one of them is double the result is in double. – gurudeb Dec 30 '12 at 17:30
  • Use `1.0/10` instead of `1/10` and it'll work just fine. – Blender Dec 30 '12 at 17:30

7 Answers7

12

1 is an int and 10 is an int and when you do int/int you get an int.

If you do 1.0/10 or 1/10.0 or 1.0/10.0 you will get a double as 0.1

IMHO: I think int/int should be a double which you can cast an int if you want integer division. i.e.

I would have

 a/b          // does double division
 (int)(a/b)   // does integer division.

instead you have to write

 a/b          // does integer division
 (double) a/b // does double division
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • 3
    +1 for the catchy first line :) – Marko Topolnik Dec 30 '12 at 17:31
  • A double result may be more expected, but traditionally processors provide integer division which is quite a bit faster than double division (and doesn't need conversions), and the C family of languages integer division feature gives access to that. – Erik Eidt Dec 30 '12 at 17:44
  • @ErikEidt I agree that integer division is faster and you should have a way of doing it, but I don't think it should be the default. You should have to say this is what you want becaus eyou know what you are doing, so there is less suprises if you haven't thought about it. – Peter Lawrey Dec 30 '12 at 17:47
  • @Peter, yup, makes sense – Erik Eidt Dec 30 '12 at 17:49
6

It doesn't cast to int, it already is int. int / int is int.

Also note that when you cast to double as in (double)1/10, since the cast operator has higher precedence than division, this is the same as ((double)1)/10, which has the effect of causing the division to happen in double.

If you cast after division as in (double)(1/10), the result will be 0.0 .

Erik Eidt
  • 11,623
  • 1
  • 21
  • 39
3

One of the numbers has to be a float or a double, so try:

System.out.println(1/10.0);
System.out.println(1/(double)10);
System.out.println(1.0/10);
Jesper Fyhr Knudsen
  • 7,081
  • 2
  • 32
  • 45
2

1 is an integer and 10 is an integer so by default the division is integer and thus one divided by 10 gives zero and remainder 1.

Ivaylo Strandjev
  • 64,309
  • 15
  • 111
  • 164
2

This is because, as both operands are integers, an integer division is performed, and the result of the integer division of 1 by 10 is an integer, in this case 0.

In the second case, since one argument is a double, the divisor is also converted to a double and the result will be a double.

fge
  • 110,072
  • 26
  • 223
  • 312
1

You can also do one of these:

System.out.println(1.0 / 10);
System.out.println(1 / 10.0);
System.out.println(1.0 / 10.0);

The issue is that 1 / 10 is an int expression, while your second version and each of the above is not.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
0

It's not that it was casting it to an int -- it's that it always was an int. When arithmetic is between two integers, the result is an integer as defined by integer arithmetic: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.2

yshavit
  • 39,951
  • 7
  • 75
  • 114