-2

So I had this question in a Comp Sci midterm last year and I still can't figure out why a&&b and b&&a would not evaluate to the same result

The way I see it, there are 4 possible cases :

  1. A is TRUE and B is TRUE :
    • True && True is True
  2. A is TRUE and B is FALSE :
    • True && False is False and False && True is also False
  3. A is FALSE and B is TRUE :
    • False && True is False and True && False is False
  4. A is FALSE and B is FALSE :
    • False && False is False

Yet, the correction of the midterm and the prof say that a&&b and b&&a don't always evaluate to the same result.

EDIT: I don't see how this is a duplicate of "operator short-circuiting" ... in no way am I asking a question about the first part of the AND operation ... I am asking about both parts and the "original" question to which this is a duplicate does not answer my question.

SilenT612
  • 43
  • 1
  • 7
  • 3
    Depends what `a` and `b` would represent. It could be possible that either `a` or `b` could modify the state of the other expression – SomeJavaGuy Jul 27 '16 at 08:42
  • 5
    If a and b are variables, then of course a&&b and b&&a **must** give the same result. But if those are actually method calls, things are different (because of side effects). – GhostCat Jul 27 '16 at 08:43
  • Well, **authority** should not be understood to replace **reason**. In other words: tell your professor that he should either prove his statement, or accept that he is wrong. Maybe you find politer words for that then me. – GhostCat Jul 27 '16 at 08:45
  • stating that he is wrong is pretty much a death sentence for him. Quite a few academics are completely disconnected from reality **and** suffer from not-so-negligible megalomania. Instead, he should **ask** if he understood his statement correctly and **ask** why. Students are only allowed to **ask**, most certainly not to *doubt* - well ... they can do whatever they want but if the prof doesnt want student X in his course ... well ... he might give him some problems – specializt Jul 27 '16 at 08:47
  • 1
    First, I don't understand why OldCurmudgeon marked this question as a duplicate ... of "operator short-circuiting" ? ... Second, thanks a lot GhostCat, I should have though of method calls but correct me if I'm wrong but does it mean that for example, if one calls method a and that the method modifies the output of b and not the other way around, then a&&b and b&&a would indeed not evaluate to the same result ? – SilenT612 Jul 27 '16 at 08:50
  • 1
    @SilenT612 imagine two variables `a` and `b`, both set to `false`, and two methods `a1` and `b1`. `a1` would simply return `a`. `b1` would set `a` and `b` to `true` and return `b`. In fact `a1&&b1` would be `false` now, since `a1` would return the current state of `a`, which is `false`. But `b1&&a1` would modify `a` to `true` and `b` to `true` which would result in an overall `true` statement. – SomeJavaGuy Jul 27 '16 at 08:55
  • Yes, the result of the if is the same, but this doesn't mean it doesn't have an impact. for instance: if ((i - 1 < 3) && ((j += 1) > 2)) and if (((j += 1) > 2) && (i - 1 < 3)) will always be 'true' or 'false' the same, but the 'result' of value of j is different for both. – Stultuske Jul 27 '16 at 09:00
  • 1
    @SilenT612 Operator short-circuiting is relevant here as it is one reason why the two statements might not be the same. When using a && b, the expression is evaluated from left to right. If a evaluates to false, then b will not be evaluated, since the Java VM will already know that the expression will be false. However if a evaluates to true, then b will also be evaluated. Therefore a && b is sometimes used as short-hand for writing: if(a) { b; }. Its therefore clear that the order of a and b does in fact matter when a and/or b are something other than simple boolean constants or variables. – mdewit Jul 27 '16 at 09:07

1 Answers1

0

Yes you will get the same result. But if you have a&&b and a is FALSE then Java won't evaluate b and will say that result is FALSE.

Mikhail
  • 17
  • 3