__Primitive data-types in Java V - Booleans__

*The sun is green. The ocean is blue.*Two English language statements where the first is (obviously) false, and the second true. In Java, a

primitive data-type called a

*boolean*is used to store such kind of information (truth) about statements. Java boolean variables are usually used to

store the results of conditional statements (the truth of a statement), where a boolean can have only two values

*true*and

*false*.

The size of a boolean is undefined, but in certain implementations of Java, it is 1-bit. Nevertheless, it should not affect the programming or

the performance of the program.

__Conditional statements__

Boolean values (true and false) in java are usually the result of performing Equality/Relational/logical operations on other variables. These operations range from

value comparisons (less than, more than, equal, less than or equal, more than or equal), to logical operations on boolean variables

( And , Or , Not).

value comparisons (less than, more than, equal, less than or equal, more than or equal), to logical operations on boolean variables

( And , Or , Not).

__Equality and Relational Operators__

Similar to Mathematics, Java also has comparative operators that enable programmers to compare numeric values with each other.

For example, the "x less than y" statement in maths is represented as "x < y" in Java. The following table summarizes the different

comparative operators available in Java.

For example, the "x less than y" statement in maths is represented as "x < y" in Java. The following table summarizes the different

comparative operators available in Java.

Operator | Example | Equivalent to |
---|---|---|

== | x == y | true; if x is equal to y. False otherwise |

!= | x != y | true; if x is NOT equal to y. False otherwise |

> | x > y | true; if x is more than y. False otherwise |

< | x < y | true; if x is less than y. False otherwise |

>= | x >= y | true; if x is more than OR equal to y. False otherwise |

<= | x <= y | true; if x is less than OR equal to y. False otherwise |

The following program is an illustration of how the equality and relational operators behave in Java.

public class EqRel_Tutorial { public static void main(String[]args){ int x = 3; int y = 8; boolean useless = true; boolean lessThan = x < y; boolean moreThan = x > y; System.out.println(lessThan); // true System.out.println(moreThan); // false System.out.println( x >= 3 ); //true System.out.println( x <= 3 ); //true System.out.println( x != y ); //true System.out.println( x == y ); //false } }

The program above is an example of how equality and relational operations evaluate to a boolean value, either true or false. A boolean value can be saved

as in the case of the variables

as in the case of the variables

*lessThan*and*moreThan*. The boolean values can also be printed directly as in the subsequent cases. Take a look at the results below, and compare them with the statements in the table.true false true true true false

__Conditional Operators__

Conditional operators in Java allow us to build larger and more comprehensive boolean expressions from smaller expressions, by the use of the

*AND*,*OR, NOT*and*XOR*operators. The conditional operators evaluate to a certain boolean value, based on the boolean expressions to its left and its right. The following table summarizes the conditional operators and their meanings:Operator | Example | Equivalent to |
---|---|---|

&& | x && y | AND operator. true; if both x and y are true |

|| | x || y | OR operator. true; if at least one of x or y is true (one or both of them are true) |

^ | x ^ y | XOR operator. true; if either x or y is true, but not both. |

! | !x | NOT operator. true; if x is false; false if x is true |

The following program is an example of how conditional operators are used to connect two or more boolean values/expressions together, to

form new boolean values.

form new boolean values.

public class ConditionalTutorial { public static void main(String[]args){ int a = 3; int b = -1; int c = 7; boolean aLessThanB = a < b; boolean aLessThanC = a < c; boolean aNotLessThanB = !( a < b ); boolean aLessThanBOrC = aLessThanB || aLessThanC; boolean aLessThanBAndC = aLessThanB && aLessThanC; boolean aLessThanEitherBOrC = (a < b) ^ (a < c); // check if a less than b or c, but not both. System.out.println(aLessThanB); //false System.out.println(aNotLessThanB); //true System.out.println(aLessThanC); //true System.out.println(aLessThanBOrC); //true System.out.println(aLessThanBAndC); //false System.out.println(aLessThanEitherBOrC); //true } }

As you can see here, smaller boolean expressions were build first, as in the case of "

expressions/values were later combined using the conditional operators. Note that we do not have to first store an expression in a boolean variable, as

boolean expressions could evaluated right away as in the case of "

manage the order of the evaluation of the expression, i.e., the expressions enclosed within the parenthesis are first evaluated, and later, the outer

"super-expression" is evaluated. For example, in the case of

would have the NOT operator applied on it. The following are the results obtained from running the program.

**boolean aLessThanB = a < b;" ,**and these smaller booleanexpressions/values were later combined using the conditional operators. Note that we do not have to first store an expression in a boolean variable, as

boolean expressions could evaluated right away as in the case of "

*!( a < b)"*and*" (a < b) ^ (a < c)".*The parenthesis (brackets) here are used tomanage the order of the evaluation of the expression, i.e., the expressions enclosed within the parenthesis are first evaluated, and later, the outer

"super-expression" is evaluated. For example, in the case of

*" ! (a < b) ",*we force java to first evaluate*" a < b ",*and later, the result of that evaluationwould have the NOT operator applied on it. The following are the results obtained from running the program.

false true true true false true

Boolean values are usually used for use with conditional statements and loops. These will statements will be discussed later, but next, we will discuss the

last section of the primitive data-types tutorial, Strings.

last section of the primitive data-types tutorial, Strings.