Java's data types include boolean, the type named for the creator of the algebraic logic system that serves as its base, George Boole. Boolean logic has many uses, one of which is comparison between some number of objects in Java. Other common uses include Venn diagrams, mathematical truth tables, and search engines.
Boolean Variables
Boolean with an uppercase B in Java refers to a class filled with methods involving boolean operations, while lowercase boolean refers to primitive values. The possible values of a boolean variable is simple: true or false. Unlike C++, values like 1 or 0 cannot be used as equivalents to true or false, nor can abbreviations like 't' or 'f'.
A boolean variable is declared like so:
- boolean var=true;
- boolean var2=false;
But boolean variables are far more useful when used with comparing values with relational and logical operators:
public class Example {
public static void main (String[] args) {
int var1 = 3;
int var2 = 5;
boolean check=(var1<var2);
System.out.println("Is var1 less than var2?");
System.out.println(check);
}
}
Java gives the boolean variable a true or false based on the comparison of var1 to var2 using a relational operator. The integer variable var1 happened to be less than var2, so the boolean variable check was set to 'true' and was used to print to the screen.
Relational operators
Some operators are typically used to compare two primitive values. These include the regular comparison operators:
- == or "is equal to". 1==2 would return a 'false'.
- != or "is not equal to". 1!=2 would return a 'true'.
- > or "is greater than". 1>2 would return a 'false'.
- < or "is less than". 1<2 would return a 'true'.
- >= or "is greater than or equal to". 1>=2 would return a 'false'.
- <= or "is less than or equal to". 1<=2 would return a 'true'.
A single = is not used for "is equal to" because it is reserved for assignment statements or assigning values to variables. These operators compare values or variables, but they do not change or assign new values to those variables. The single = sign can be used to set variables equal to some value, which can then be compared with the relational operators, like “var1>=var2”, etc.
Logical Operators
The boolean logic also involves the use of logical operators such as AND, OR, and NOT. These logical operators are useful to compare more than two values:
- && or "and" requires all compared values to be true. For example, if x=1 and y=2 and the program states "x<y && y>3", the comparison will be false. Y is not greater than 3 even though x is less than y.
- or “or” requires only that at least one value is true in order for the entirety to be true. For example, “x<y || y>3” would return as true since at least one expression is true. Note that both expressions can be true, and the comparison will only return false when all expressions are false; “x>y || y>3” would return as false.
- ! or “not” reverses a boolean expression. If “x>y||y>3” returns a false, !(x>y||y>3) returns a true.
These operators allow faster comparison of multiple expressions and so are called 'short-circuiting'.
Conclusion
This tutorial explains some of the concepts involved with comparing boolean values, usually of the primitive variety. Although the Boolean class has several useful class methods, the primitive use is probably the most common. The comparison of boolean variables has many uses, the most obvious being its use as a test for control structures like branching and loops.