Boolean expressions
- 100% developed as of Dec 31, 2012 Statements
- 100% developed as of Mar 10, 2013 Conditional blocks
- 100% developed as of Mar 10, 2013 Loop blocks
- 100% developed as of May 24, 2013 Boolean expressions
- 100% developed as of Feb 16, 2010 Variables
- 100% developed as of Mar 10, 2013 Primitive Types
- 100% developed as of Mar 10, 2013 Arithmetic expressions
- 100% developed as of May 24, 2013 Literals
- 100% developed as of Mar 10, 2013 Methods
- 100% developed as of May 24, 2013 String
- 100% developed as of Mar 10, 2013 Objects
- 100% developed as of Jul 5, 2012 Packages
- 100% developed as of Mar 10, 2013 Arrays
- 75% developed as of Jan 11, 2013 Mathematical functions
- 75% developed as of Jan 11, 2013 Large numbers
- 75% developed as of Jan 11, 2013 Random numbers
- 100% developed as of Apr 8, 2013 Unicode
- 100% developed as of Apr 8, 2013 Comments
- 100% developed as of Sep 27, 2007 Keywords
- 100% developed as of Aug 6, 2013 Coding conventions
- 0% developed as of Mar 26, 2018 Lambda expressions
Boolean values are values that evaluate to either true
or false
, and are represented by the boolean
data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".
Comparative operators
[edit | edit source ]Java has several operators that can be used to compare variables. For example, how would you tell if one variable has a greater value than another? The answer: use the "greater-than" operator.
Here is a list of the comparative operators in Java:
>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to==
: Equal to!=
: Not equal to
To see how these operators are used, look at this example:
inta=5,b=3; System.out.println(a>b);// Value is true because a is greater than b System.out.println(a==b);// Value is false because a does not equal b System.out.println(a!=b);// Value is true because a does not equal b System.out.println(b<=a);// Value is true because b is less than a
true false true true
Comparative operators can be used on any primitive types (except boolean
), but only the "equals" and "does not equal" operators work on objects. This is because the less-than/greater-than operators cannot be applied to objects, but the equivalency operators can.
==
and !=
operators test whether both variables point to the same object. Objects will be covered later in the tutorial, in the "Classes, Objects, and Types" module.
Boolean operators
[edit | edit source ]The Java boolean operators are based on the operations of the boolean algebra. The boolean operators operate directly on boolean values.
Here is a list of four common boolean operators in Java:
!
: Boolean NOT&&
: Boolean AND||
: Boolean inclusive OR^
: Boolean exclusive XOR
The boolean NOT operator ("!") inverts the value of a boolean expression. The boolean AND operator ("&&") will result in true if and only if the values on both sides of the operator are true. The boolean inclusive OR operator ("||") will result in true if either or both of the values on the sides of the operator is true. The boolean exclusive XOR operator ("^") will result in true if one and only one of the values on the sides of the operator is true.
To show how these operators are used, here is an example:
booleaniMTrue=true; booleaniMTrueToo=true; booleaniMFalse=false; booleaniMFalseToo=false; System.out.println("NOT operand:"); System.out.println(!iMTrue); System.out.println(!iMFalse); System.out.println(!(4<5)); System.out.println("AND operand:"); System.out.println(iMTrue&&iMTrueToo); System.out.println(iMFalse&&iMFalseToo); System.out.println(iMTrue&&iMFalse); System.out.println(iMTrue&&!iMFalse); System.out.println("OR operand:"); System.out.println(iMTrue||iMTrueToo); System.out.println(iMFalse||iMFalseToo); System.out.println(iMTrue||iMFalse); System.out.println(iMFalse||!iMTrue); System.out.println("XOR operand:"); System.out.println(iMTrue^iMTrueToo); System.out.println(iMFalse^iMFalseToo); System.out.println(iMTrue^iMFalse); System.out.println(iMFalse^!iMTrue);
NOT operand: false true false AND operand: true false false true OR operand: true false true false XOR operand: false false true false
Here are the truth tables for the boolean operators:
a | !a |
---|---|
true | false |
false | true |
a | b | a && b | a || b | a ^ b |
---|---|---|---|---|
true | true | true | true | false |
true | false | false | true | true |
false | true | false | true | true |
false | false | false | false | false |
- For help on simplifying complex logic, see De Morgan's laws.
In Java, boolean logic has a useful property called short circuiting. This means that expressions will only be evaluated as far as necessary. In the expression (a && b)
, if a is false, then b will not be evaluated because the expression will be false no matter what. Here is an example that shows that the second expression is not automatically checked:
System.out.println((4<5)||((10/0)==2));
true
To disable this property, you can use &
instead of &&
and |
instead of ||
but it's not recommended.
- For the bitwise operations on
&
and|
, see Arithmetic expressions.