Free Java Tutorials >> Table of contents >> Booleans

1. Booleans and Boolean Operators

Declaring a boolean

The boolean variable type stores either true or false, and it can be nothing else. Unlike in languages like C or C++, in Java, a boolean cannot be 0, 1, 4.2, "Hello", etc. You can declare a boolean like so:

public class MyProgram {
    public static void main(String[] args) {
		boolean c = true;
		System.out.println(c); //outputs true
    }
}

The boolean NOT (!) operator

To invert (flip) a boolean, put an exclamation mark in front of the variable or expression:

public class MyProgram {
    public static void main(String[] args) {
		System.out.println(!true);   //prints false
		System.out.println(!false);  //prints true
		System.out.println(!!true);  //prints true
		System.out.println(!!!true); //prints false
    }
}

Boolean operators AND and OR (&& ||)

The AND (&&) and OR (||) operators require two booleans on either side. In the case of the AND, if both sides are true then the expression is true. In the case of the OR, if either side is true, then the expression is true:

public class MyProgram {
    public static void main(String[] args) {
		System.out.println(true && false);  //false
		System.out.println(true && true);   //true
		System.out.println(false && false); //false
		System.out.println(true || false);  //true
		System.out.println(true || true);   //true
		System.out.println(false || false); //false
    }
}
//Note that A AND B evaluates to the same as B AND A
//Also A OR B evaluates to the same as B OR A

Note on operator order of && and ||: AND is run before OR

Advanced students should note that these operators are short circuiting. If the left side of an AND is false, Java will not even try to evaluate the right side and will immediately return false. If the left side of an OR is true, Java will immediately return true without evaluating the right side. We will discuss short circuiting more later.



			
Run

2. Comparison Operators and Order of Operations

Numeric boolean operators

The true power of booleans is our ability to make an expression that evaluates into a boolean. Just like 1+3 is an int expression that evaluates to the int 4, 5 > 3 is a boolean expression. Five is greater than three evaluates to the value true because 5 is indeed greater than 3. 5 < 3 is false:

public class MyProgram {
    public static void main(String[] args) {
		System.out.println(5 > 3);
		boolean d = 5 < 3; //as long as the right and left hand types...
		System.out.println(d);        //... match you can assign variables
		System.out.println(4 >= 4);   //true if greater or equal
		System.out.println(5 <= 3);   //outputs false
		System.out.println(5 == 3);   //true if 5 is equal to 3 (in this case no)
		System.out.println(5 != 3);   //true if 5 is not equal to 3 (in this case yes)
    }
}

Operator Precedence

Now that we have seen more operators, we can look at a full list of the Java order of operations. Some of these might be new to you. Most programmers do not memorize this table, and even then it is important to use parenthesis to make code more understandable. The most important to memorize are: Modulo is equal to multiplication and division, and OR is after AND.

Operator Description Level Associativity
[]
.
()
++
--
access array element
access object member
invoke a method
post-increment
post-decrement
1 left to right
++
--
+
-
!
~
pre-increment
pre-decrement
unary plus
unary minus
logical NOT
bitwise NOT
2 right to left
()
new
cast
object creation
3 right to left
*
/
%
multiplicative 4 left to right
+ -
+
additive
string concatenation
5 left to right
<< >>
>>>
shift 6 left to right
<  <=
>  >=
instanceof
relational
type comparison
7 left to right
==
!=
equality 8 left to right
& bitwise AND 9 left to right
^ bitwise XOR 10 left to right
| bitwise OR 11 left to right
&& conditional AND 12 left to right
|| conditional OR 13 left to right
?: conditional 14 right to left
  =   +=   -=
 *=   /=   %=
 &=   ^=   |=
<<=  >>= >>>=
assignment 15 right to left


			
Run

3. Compound Boolean Expressions

Using && and || in more complex expressions

Because expressions can build upon each other, you can do 5 > 3 || 3 == 4 and it will first evaluate to true || 3 == 4 and then true || false. The final value is true. Here are some more examples of boolean expressions

public class MyProgram {
    public static void main(String[] args) {
		System.out.println( 5 > 3 || 5 < 3);
		int age = 5;
		boolean bornInUSA = true;
		boolean canBeAmericanPresident = age >= 18 && bornInUSA; 
		System.out.println( canBeAmericanPresident || age == -1); //murp
		System.out.println(1==1&&2==3||4==4&&3==5); //AND is run before OR
	}
}

Note you cannot do x > 5 || 3 (even though in english you may say x is greater than 5 or 3). This is because x > 5 || 3 simplifies to something like true || 3. The right side is now just a number, but it needs to be a boolean, and so this will not run

Divisibility

To check whether x is divisible by y, you need to see whether there is no remainder when x divides y. To do this, we use both the modulo and equality operator:

public class MyProgram {
    public static void main(String[] args) {
		int x = 10;
		int y = 2;
		System.out.println("Is 10 divisible by 2?");
		System.out.println(x%y == 0); //true
		y = 3;
		System.out.println("Is 10 divisible by 3?");
		System.out.println(x%y == 0); //false
	}
}


			
Run

Previous: Primitives

Next: If Statements