[Solved] confusion between && and || in java


  • && is a logical and operator

  • || is the logical or operator

Using De Morgan, the following:

while(!gender.equals(MALE) && !gender.equals(FEMALE))

Can be translated to:

while(!(gender.equals(MALE) || gender.equals(FEMALE)))

(note the additional parenthesis and the placement of the ! before them).

Both the above mean that the gender is neither MALE or FEMALE.

Your other code:

while(!gender.equals(MALE) || !gender.equals(FEMALE))

The above means – gender is not MALE or gender is not FEMALE.

while(gender.equals(MALE)  ||  gender.equals(FEMALE));

Similarly, the above means – gender is MALE or gender is FEMALE.

3

solved confusion between && and || in java