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

后端 未结 8 1108
太阳男子
太阳男子 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:54

    In c and C++ there is no data type called BOOLEAN Thats why it uses 1 and 0 as true false value. and in JAVA 1 and 0 are count as an INTEGER type so it produces error in java. And java have its own boolean values true and false with boolean data type.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题