user6264026 - 8 months ago 18

Java Question

I'm a high school student currently getting ready for a state academic meet(UIL). I have a problem and I've looked everywhere and can't seem to find an answer! Why does this print out 0.0?

`double d = 1/2;`

System.out.println(d);

Answer

It's because of the data type.

When you do `1/2`

that is integer division because two operands are integers, hence it resolves to zero (0.5 rounded down to zero).

If you convert any one of them to double, you'll get a double result.

```
double d = 1d/2;
```

or

```
double d = 1/2.0;
```