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

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

    Because booleans have two values: true or false. Note that these are not strings, but actual boolean literals.

    1 and 0 are integers, and there is no reason to confuse things by making them "alternative true" and "alternative false" (or the other way round for those used to Unix exit codes?). With strong typing in Java there should only ever be exactly two primitive boolean values.

    EDIT: Note that you can easily write a conversion function if you want:

    public static boolean intToBool(int input)
    {
       if (input < 0 || input > 1)
       {
          throw new IllegalArgumentException("input must be 0 or 1");
       }
    
       // Note we designate 1 as true and 0 as false though some may disagree
       return input == 1;
    }
    

    Though I wouldn't recommend this. Note how you cannot guarantee that an int variable really is 0 or 1; and there's no 100% obvious semantics of what one means true. On the other hand, a boolean variable is always either true or false and it's obvious which one means true. :-)

    So instead of the conversion function, get used to using boolean variables for everything that represents a true/false concept. If you must use some kind of primitive text string (e.g. for storing in a flat file), "true" and "false" are much clearer in their meaning, and can be immediately turned into a boolean by the library method Boolean.valueOf.

    0 讨论(0)
  • 2020-12-01 05:29

    Java, unlike languages like C and C++, treats boolean as a completely separate data type which has 2 distinct values: true and false. The values 1 and 0 are of type int and are not implicitly convertible to boolean.

    0 讨论(0)
  • 2020-12-01 05:33

    On a related note: the java compiler uses int to represent boolean since JVM has a limited support for the boolean type.See Section 3.3.4 The boolean type.

    In JVM, the integer zero represents false, and any non-zero integer represents true (Source : Inside Java Virtual Machine by Bill Venners)

    0 讨论(0)
  • 2020-12-01 05:42

    Being specific about this keeps you away from the whole TRUE in VB is -1 and in other langauges true is just NON ZERO. Keeping the boolean field as true or false keeps java outside of this argument.

    0 讨论(0)
  • 2020-12-01 05:43

    Even though there is a bool (short for boolean) data type in C++. But in C++, any nonzero value is a true value including negative numbers. A 0 (zero) is treated as false. Where as in JAVA there is a separate data type boolean for true and false.

    0 讨论(0)
  • 2020-12-01 05:48

    One thing that other answers haven't pointed out is that one advantage of not treating integers as truth values is that it avoids this C / C++ bug syndrome:

    int i = 0;
    if (i = 1) {
        print("the sky is falling!\n");
    } 
    

    In C / C++, the mistaken use of = rather than == causes the condition to unexpectedly evaluate to "true" and update i as an accidental side-effect.

    In Java, that is a compilation error, because the value of the assigment i = 1 has type int and a boolean is required at that point. The only case where you'd get into trouble in Java is if you write lame code like this:

    boolean ok = false;
    if (ok = true) {  // bug and lame style
        print("the sky is falling!\n");
    }
    

    ... which anyone with an ounce of "good taste" would write as ...

    boolean ok = false;
    if (ok) {
        print("the sky is falling!\n");
    }
    
    0 讨论(0)
提交回复
热议问题