-1

I am an experienced php developer just starting to learn Java. I am following some Lynda courses at the moment and I'm still really early stages. I'm writing sample programs that ask for user input and do simple calculation and stuff.

Yesterday I came across this situation:

double result = 1 / 2;

With my caveman brain I would think result == 0.5, but no, not in Java. Apparantly 1 / 2 == 0.0. Yes, I know that if I change one of the operands to a double the result would also be a double.

This scares me actually. I can't help but think that this is very broken. It is very naive to think that an integer division results in an integer. I think it is even rarely the case.

But, as Java is very widely used and searching for 'why is java's division broken?' doesn't yield any results, I am probably wrong.

My questions are:

  1. Why does division behave like this?
  2. Where else can I expect to find such magic/voodoo/unexpected behaviour?
Dennis Haarbrink
  • 3,738
  • 1
  • 27
  • 54
  • 2
    Because 1 and 2 are integers, use 1.0, 1f, or do an implicit cast. It behaves like this by design, and you'll expect this whenever you're dealing with integer arithmetic. [Here](http://ideone.com/MZIyBm) are some of the options you can try. – Dave Chen Dec 08 '16 at 07:31
  • Going from a loose language (php) to a strongly typed language (java), you'll need to get used to the fact that an integer stays an integer unless you tell it otherwise. – Dave Chen Dec 08 '16 at 07:39
  • Why the downvote? I think the question was stated clearly. If you think otherwise, please let me know and I will update it. – Dennis Haarbrink Dec 08 '16 at 07:47
  • I didn't downvote, but it seems like you're just asking why Java was designed like this, rather than why this code does something other than what you expected. – Dave Chen Dec 08 '16 at 07:50
  • 1
    It isn't broken. It is just different. So of course when you search for "java division broken" you don't get any useful results. Same as if you searched for "polar bear eating penguin". – Stephen C Dec 08 '16 at 07:59
  • Not only the language, but every computer I've ever used that has a hardware instruction to divide integers would produce an integer answer. Because integer arithmetic is integer arithmetic! –  Oct 04 '19 at 20:36

6 Answers6

5

Java is a strongly typed language so you should be aware of the types of the values in expressions. If not...

1 is an int (as 2), so 1/2 is the integer division of 1 and 2, so the result is 0 as an int. Then the result is converted to a corresponding double value, so 0.0.

Integer division is different than float division, as in math (natural numbers division is different than real numbers division).

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
4

You are thinking like a PHP developer; PHP is dynamically typed language. This means that types are deduced at run-time, so a fraction cannot logically produce a whole number, thus a double (or float) is implied from the division operation.

Java, C, C++, C# and many other languages are strongly typed languages, so when an integer is divided by an integer you get an integer back, 100/50 gives me back 2, just like 100/45 gives me 2, because 100/45 is actually 2.2222..., truncate the decimal to get a whole number (integer division) and you get 2.

In a strongly typed language, if you want a result to be what you expect, you need to be explicit (or implicit), which is why having one of your parameters in your division operation be a double or float will result in floating point division (which gives back fractions).

So in Java, you could do one of the following to get a fractional number:

double result = 1.0 / 2;
double result = 1f / 2;
double result = (float)1 / 2;

Going from a loosely typed, dynamic language to a strongly typed, static language can be jarring, but there's no need to be scared. Just understand that you have to take extra care with validation beyond input, you also have to validate types.

Going from PHP to Java, you should know you can not do something like this:

$result = "2.0";
$result = "1.0" / $result;
echo $result * 3;

In PHP, this would produce the output 1.5 (since (1/2)*3 == 1.5), but in Java,

String result = "2.0";
result = "1.0" / result;
System.out.println(result * 1.5);

This will result in an error because you cannot divide a string (it's not a number).

Hope that can help.

txtechhelp
  • 6,625
  • 1
  • 30
  • 39
  • 1
    Exactly. You can't just divide strings in Java, but in PHP -- no problem! – Dave Chen Dec 08 '16 at 07:51
  • Ok, so what I learn from all these answers basically boils down to: 1. it is by design, and 2. it is desirable behaviour. My conclusion: accept and embrace. – Dennis Haarbrink Dec 08 '16 at 08:15
  • 1
    @DennisHaarbrink .. pretty much :) Though I will add to your point #2 .. there's nothing _wrong_ with dynamic languages like PHP (or Lua, Pearl, JavaScript, etc.), but understanding the limits of the language, and more importantly **why** the limits are there helps to ensure you build a more robust application .. like how Java doesn't have `unsigned int`, so if you want to hold a value larger than +/- 2.1 million, you'd need to use a `long` – txtechhelp Dec 08 '16 at 08:28
0

I'm by no means a professional on this, but I think it's because of how the operators are defined to do integer arithmetic. Java uses integer division in order to compute the result because it sees that both are integers. It takes as inputs to this "division" method two ints, and the division operator is overloaded, and performs this integer division. If this were not the case, then Java would have to perform a cast in the overloaded method to a double each time, which is in essence useless if you can perform the cast prior anyways.

rb612
  • 5,280
  • 3
  • 30
  • 68
0

If you try it with c++, you will see the result is the same. The reason is that before assigning the value to the variable, you should calculate it. The numbers you typed (1 and 2) are integers, so their memory allocation should be as integers. Then, the division should done according to integers. After that it will cast it to double, which gives 0.0.

Bünyamin Sarıgül
  • 3,031
  • 4
  • 30
  • 55
0

Why does division behave like this?

Because the language specification defines it that way.

Where else can I expect to find such magic/voodoo/unexpected behaviour?

Since you're basically calling "magic/voodoo" something which is perfectly defined in the language specification, the answer is "everywhere".

So the question is actually why there was this design decision in Java. From my point of view, int division resulting in int is a perfectly sound design decision for a strongly typed language. Pure int arithmetic is used very often, so would an int division result in float or double, you'd need a lot of rounding which would not be good.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • At some point, one expects the Java virtual machine to be mapped onto real hardware, and "integer divided by integer gives integer" is pretty much standard operation; I don't think I've seen "integer divided by integer gives floating-point", though I suppose an FPU could do that. –  Oct 04 '19 at 20:41
-1
package demo;

public class ChocolatesPurchased
{
    public static void main(String args[])
    {
        float p = 3;
        float cost = 2.5f;
        p *= cost;
        System.out.println(p);
    }
}
Patrick Burwell
  • 129
  • 1
  • 12