Boolean expressions in Java

后端 未结 8 1066
不知归路
不知归路 2020-12-21 06:00

I have a question about the meaning (evaluation) of Boolean variables in return statements in Java.

I know that:

if (var) { ... }

i

相关标签:
8条回答
  • 2020-12-21 06:28

    Yes, this is true for all booleans. You can think of if(expression) evaluating 'expression' to see if it's 'true' or 'false'. When you do

    if(b < a == true)
    

    it first tests to see if b < a and if it is, it now tests:

    if(true == true)
    

    It now tests whether true == true (which it obviously does). Java isn't doing anything tricky when you leave out the extra '== true', it just needs to perform one fewer test. There's no reason you couldn't say:

    if(((b < a == true) == true) == true)
    

    but it would cause Java to perform an extra test each time it sees an equals sign.

    0 讨论(0)
  • 2020-12-21 06:32

    Just like C++ every statement has a return value, even things on the left hand side of the operator. Some of the crazier C++ I've seen has operations on the left hand side with their results being assigned to.

    I find it works better if I do the following:

    bool result = (b > a);
    return result;
    

    The reason only being that is is a bit easier to debug (in just about any IDE). I find that I always wrap it in parenthesis, I'm not quite sure why. I think it keeps the variable electrons warm while they sleep at night.

    0 讨论(0)
  • 2020-12-21 06:38

    Don't needlessly complicate your code. If you feel the need to say "a < b == true", then you can follow that to its logical conflusion (conclusion + confusion) and say "((((((((...(a<b) == true) == true).... == true)"

    "a < b" is a boolean expression. If you already have a boolean, why compare it to another boolean? You're not making it more boolean that way.

    0 讨论(0)
  • 2020-12-21 06:39

    Your method will work, but it can be a bit unclear what exactly should happen, especially if you just have variables named a and b. You want to document the method and have variables with proper names.

    Also, if the code is confusing to you just after you wrote it, think of someone who will come in 6 months and won't have any idea what is going on. Proper documentation and comments will help greatly.

    0 讨论(0)
  • 2020-12-21 06:40

    It's not an "implicit assumption," it's what the compiler's doing. The b < a is just an expression, the same as if it were used for an if statement. The expression evaluates to a boolean, which is then returned.

    Also noteworthy, you seem to interchange boolean and Boolean as though they're the same, but they're actually not. boolean is the primitive form while Boolean is an Object that wraps a boolean.

    0 讨论(0)
  • 2020-12-21 06:40

    Your confusion might be eased if you try thinking about operators as methods. Using your example, you had the < "less than" operator. For our purposes, the < operator can really be considered a "method" (it only doesn't look like one), which takes two parameters and returns a boolean result. If the "method" were named lessThan, your example would be equivalent to this:

    public boolean lookBetter() {     
      return lessThan(b, a);     
    } 
    

    Perhaps seeing it like that makes it a tad easier to understand? Incidentally, when I was leading exercise groups in the 'Programming 101' course back in Uni, this proved to be by far the hardest thing to teach, and a lot of people had trouble grasping the concepts involved. It almost seemed to be akin to learning to ride a bike or to swim, once you get how it works it becomes self-evident in a way.

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