Why boolean in Java takes only true or false? Why not 1 or 0 also?

后端 未结 8 1140
太阳男子
太阳男子 2020-12-01 05:28

Is there any reason why Java booleans take only true or false why not 1 or 0 also?

8条回答
  •  星月不相逢
    2020-12-01 05:55

    Because the people who created Java wanted boolean to mean unambiguously true or false, not 1 or 0.

    There's no consensus among languages about how 1 and 0 convert to booleans. C uses any nonzero value to mean true and 0 to mean false, but some UNIX shells do the opposite. Using ints weakens type-checking, because the compiler can't guard against cases where the int value passed in isn't something that should be used in a boolean context.

提交回复
热议问题