Java Tutorial/Operators/Logical Operators

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

AND operator

public class MainClass {
  public static void main(String[] arg) {
    int a = 0;
    int b = 1;
    if (a == 0 && b == 1) {
      System.out.println("here");
    } else {
      System.out.println("there");
    }
  }
}



here


Boolean Logical Operators

OperatorResult&Logical AND|Logical OR^Logical XOR (exclusive OR)||Short-circuit OR&&Short-circuit AND!Logical unary NOT&=AND assignment|=OR assignment^=XOR assignment==Equal to!=Not equal to?:Ternary if-then-else


Boolean NOT Operations: applies to one boolean operand

public class MainClass {
  public static void main(String[] arg) {
    int a = 0;
    if (!(a == 0)) {
      System.out.println("a is not 0");
    } else {
      System.out.println("a is 0");
    }
  }
}



a is 0


Demonstrates short-circuiting behavior

public class MainClass {
  static boolean test1(int val) {
    System.out.println("test1(" + val + ")");
    System.out.println("result: " + (val < 1));
    return val < 1;
  }
  static boolean test2(int val) {
    System.out.println("test2(" + val + ")");
    System.out.println("result: " + (val < 2));
    return val < 2;
  }
  static boolean test3(int val) {
    System.out.println("test3(" + val + ")");
    System.out.println("result: " + (val < 3));
    return val < 3;
  }
  public static void main(String[] args) {
    if (test1(0) && test2(2) && test3(2))
      System.out.println("expression is true");
    else
      System.out.println("expression is false");
  }
}



test1(0)
result: true
test2(2)
result: false
expression is false


Demonstrate the boolean logical operators

public class MainClass {
  public static void main(String args[]) {
    boolean a = true;
    boolean b = false;
    boolean c = a | b;
    boolean d = a & b;
    boolean e = a ^ b;
    boolean f = (!a & b) | (a & !b);
    boolean g = !a;
    System.out.println("        a = " + a);
    System.out.println("        b = " + b);
    System.out.println("      a|b = " + c);
    System.out.println("      a&b = " + d);
    System.out.println("      a^b = " + e);
    System.out.println("!a&b|a&!b = " + f);
    System.out.println("       !a = " + g);
  }
}



a = true
b = false
a|b = true
a&b = false
a^b = true
!a&b|a&!b = true
!a = false


Logical Operators

  1. You use logical operators to Combine a number of conditions.
  2. Logical operators operate on boolean values.

SymbolLong Name&logical AND&&conditional AND|logical OR||conditional OR!logical negation (NOT)


Logical operators in action

public class MainClass
{
   public static void main( String args[] )
   {
      // create truth table for && (conditional AND) operator
      System.out.printf( "%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n",
         "Conditional AND (&&)", "false && false", ( false && false ),
         "false && true", ( false && true ), 
         "true && false", ( true && false ),
         "true && true", ( true && true ) );
      // create truth table for || (conditional OR) operator
      System.out.printf( "%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n",
         "Conditional OR (||)", "false || false", ( false || false ),
         "false || true", ( false || true ),
         "true || false", ( true || false ),
         "true || true", ( true || true ) );
      // create truth table for & (boolean logical AND) operator
      System.out.printf( "%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n",
         "Boolean logical AND (&)", "false & false", ( false & false ),
         "false & true", ( false & true ),
         "true & false", ( true & false ),
         "true & true", ( true & true ) );
      // create truth table for | (boolean logical inclusive OR) operator
      System.out.printf( "%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n",
         "Boolean logical inclusive OR (|)",
         "false | false", ( false | false ),
         "false | true", ( false | true ),
         "true | false", ( true | false ),
         "true | true", ( true | true ) );
      // create truth table for ^ (boolean logical exclusive OR) operator
      System.out.printf( "%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n",
         "Boolean logical exclusive OR (^)", 
         "false ^ false", ( false ^ false ),
         "false ^ true", ( false ^ true ),
         "true ^ false", ( true ^ false ),
         "true ^ true", ( true ^ true ) );
      // create truth table for ! (logical negation) operator
      System.out.printf( "%s\n%s: %b\n%s: %b\n", "Logical NOT (!)",
         "!false", ( !false ), "!true", ( !true ) );
   }
}



Conditional AND (&&)
false && false: false
false && true: false
true && false: false
true && true: true
Conditional OR (||)
false || false: false
false || true: true
true || false: true
true || true: true
Boolean logical AND (&)
false & false: false
false & true: false
true & false: false
true & true: true
Boolean logical inclusive OR (|)
false | false: false
false | true: true
true | false: true
true | true: true
Boolean logical exclusive OR (^)
false ^ false: false
false ^ true: true
true ^ false: true
true ^ true: false
Logical NOT (!)
!false: true
!true: false


Logical OR Operations

The logical OR, ||, omits the evaluation of the right-hand operand when the left-hand operand is true.



public class MainClass {
  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;
    if (++value % 2 == 0 | ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}



there
9
11


The following table shows the effect of each logical operation

A         B            A | B             A & B             A ^ B              !A
   False     False        False             False             False              True
   True      False        True              False             True               False
   False     True         True              False             True               True
   True      True         True              True              False              False





&& versus &

Conditional && will not evaluate the right-hand operand if the left-hand operand is false.



public class MainClass {
  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;
    if (++value % 2 == 0 & ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}



there
9
11