Java Boolean Logical Operators

This tutorial will take you step by step through the process of understanding and using operators that act on boolean operands. The best way to learn is to compile and run these programs yourself (copy, paste, compile and run !). Comments such as /* this is a comment */ or // this is another comment are inserted to explain what does the line of code do. The programs are kept simple for the purpose of concentrating on the main idea in question.

The boolean logical operators are : | , & , ^ , ! , || , && , == , != , ?:

| the OR operator

& the AND operator

^ the XOR operator

! the NOT operator

|| the short-circuit OR operator

&& the short-circuit AND operator

== the EQUAL TO operator

!= the NOT EQUAL TO operator

?: the IF-THEN-ELSE operator

These opearators act on boolean opearands according to this table

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
Example 1: the | , & , ^ , ! opearators
  
class Bool1{ 
   public static void main(String args[]){

// these are boolean variables     
      boolean A = true;
      boolean B = false; 

      System.out.println("A|B = "+(A|B));
      System.out.println("A&B = "+(A&B));
      System.out.println("!A = "+(!A));
      System.out.println("A^B = "+(A^B));
      System.out.println("(A|B)&A = "+((A|B)&A));
    }
}

examine the output of this program using the table given above.

Example 2: the || and && opearators

these are called short-circuit operators. If you examine the table given above, you notice that if either A or B is true then A|B is true. If you use the || operator instead of the | operator and if A is true then java will not evaluate B(assuming it is a expression). The same applies to the operation A&&B, if A is false then java will not evaluate B ( assuming it is a expression) and the result is false

  
class Bool2{ 
   public static void main(String args[]){

      int x = 0;
      int y = 0;
      boolean A = true;
      boolean B = false; 

// A is true , ++x<0 will not be evaluated since the || operator is used.
      boolean C = A||(++x<0);
      System.out.println("x = "+x);
      System.out.println("C = "+C);

// ++y<0 will be evaluated since the | operator is used.
      boolean D = A|(++y<0);
      System.out.println("y = "+y);
      System.out.println("D = "+D);

// note both C and D evaluate to true.
    }
}

compile and run the above program. ++x is not evaluated since A is true and we are using the || operator, however ++y is evaluated since | is used.

Java Tips

See also
Coin Toss Programming Simulator

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.