How does Java handle integer underflows and overflows and how would you check for it?

前端 未结 12 1363
走了就别回头了
走了就别回头了 2020-11-21 06:40

How does Java handle integer underflows and overflows?

Leading on from that, how would you check/test that this is occurring?

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 07:20

    It doesn't do anything -- the under/overflow just happens.

    A "-1" that is the result of a computation that overflowed is no different from the "-1" that resulted from any other information. So you can't tell via some status or by inspecting just a value whether it's overflowed.

    But you can be smart about your computations in order to avoid overflow, if it matters, or at least know when it will happen. What's your situation?

提交回复
热议问题