29

This feels like a stupid question, but I can't find the answer anywhere in the Java documentation. If I declare two ints and then divide them, what exactly is happening? Are they converted to floats/doubles first, divided, then cast back to an integer, or is the division "done" as integers?

Also, purely from experimentation, integer division seems to round the answer towards zero (i.e. 3/2 = 1 and -3/2 = -1). Am I right in believing this?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
MadMonty
  • 807
  • 2
  • 7
  • 15
  • No conversion occurs. They are divided as ints. And `-3/2 == -1`. – Andy Turner Jun 13 '16 at 16:59
  • http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.2 – azurefrog Jun 13 '16 at 16:59
  • the simple way of thinking about it is they are divided, and anything less than a full int is truncated. – Marshall Tigerus Jun 13 '16 at 16:59
  • @AndyTurner "-3/2 = 1" was a silly typo on my part, sorry! – MadMonty Jun 13 '16 at 17:04
  • Int division in a computer basically is very similar to how you would do long division with paper and pencil to get a quotient and a remainder. The main difference is, the computer does it in base 2 instead of base 10. When you divide with '/', the result is the quotient, and the remainder is thrown away. When you divide with '%', you get the remainder, and the quotient is thrown away. – Solomon Slow Jun 13 '16 at 17:10
  • @Keiwan Not floor division. "Floor" means round downwards - but Java division is always towards zero. For negative numbers, this is the opposite of floor division. – Dawood ibn Kareem Feb 23 '17 at 20:31
  • @DavidWallace Yes, I shouldn't have called it floor division since this was a Java question. It seems like I got a little confused because "integer division" is the same as floor division in some other programming languages (Python, Wolfram Language...). But if I'm allowed to nitpick a little bit: You say "Floor means round downwards" but technically in Java rounding [DOWN](https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html#enum.constant.summary) means towards zero and UP is away from zero :) – Keiwan Feb 23 '17 at 21:17
  • Yeah, it's misnamed in Java. I meant "down" in a mathematical sense. – Dawood ibn Kareem Feb 23 '17 at 21:20

2 Answers2

38

They are being divided in integer arithmetics. So dividing integer a by integer b you get how many times b fits into a. Also a % b will give you a remainder of a division. So (a / b ) * b + a % b = a

15

Java does autoconvert types:

"It autoconverts ints to doubles. It autoconverts shorts and bytes to ints even when no ints are involved, requiring constant annoying casts when you want to do short or byte arithmetic. It autoconverts primitives to wrappers and vice versa for boxing and autoboxing." - user2357112

Java never casts anything without you specifying it.

But still integer / integer = integer.

Also, it does always truncate the result. So if the result would be 0.999999 as float the integer division would still return 0.

Community
  • 1
  • 1
Simon Kirsten
  • 2,542
  • 18
  • 21
  • No, it always rounds *towards zero*. – Andy Turner Jun 13 '16 at 17:03
  • I meant that, sorry – Simon Kirsten Jun 13 '16 at 17:04
  • I believe you meant truncate – OneCricketeer Jun 13 '16 at 17:05
  • 5
    "Java never casts anything without you specifying it" - it totally does. It autoconverts ints to doubles. It autoconverts shorts and bytes to ints even when no ints are involved, requiring constant annoying casts when you want to do short or byte arithmetic. It autoconverts primitives to wrappers and vice versa for boxing and autoboxing. – user2357112 Jun 13 '16 at 17:13
  • "It autoconverts ints to doubles" please provide an example. – Simon Kirsten Jun 13 '16 at 17:15
  • 1
    Example: double x = 15.9d / 4; – jason44107 Jun 13 '16 at 17:16
  • To add another counterexample to "Java never casts anything without you specifying it": Java even performs *narrowing conversions* without an explicit cast in certain constructs. `long v = 0; v += 3.14;` No error, not even a warning. – Matt Whitlock Jul 01 '17 at 07:11
  • @OneCricketter - "I believe you meant truncate" - The JLS uses these words: *"Integer division rounds toward 0."*. (https://docs.oracle.com/javase/specs/jls/se17/html/jls-15.html#jls-15.17.2). That *means the same thing* as truncation ... but what Andy wrote is correct. – Stephen C Dec 19 '22 at 06:49