Java math function to convert positive int to negative and negative to positive?

后端 未结 13 2170
暗喜
暗喜 2020-12-01 04:29

Is there a Java function to convert a positive int to a negative one and a negative int to a positive one?

I\'m looking for a reverse function to perfor

13条回答
  •  粉色の甜心
    2020-12-01 04:52

    Just use the unary minus operator:

    int x = 5;
    ...
    x = -x; // Here's the mystery library function - the single character "-"
    

    Java has two minus operators:

    • the familiar arithmetic version (eg 0 - x), and
    • the unary minus operation (used here), which negates the (single) operand

    This compiles and works as expected.

提交回复
热议问题