Why is Java's division broken?

前端 未结 6 1780
慢半拍i
慢半拍i 2020-11-27 08:59

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 progra

6条回答
  •  伪装坚强ぢ
    2020-11-27 09:19

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

提交回复
热议问题