Java
Java Booleans
Java Booleans
A boolean type is Very often, in programming, you will need a data
declared with type that can only have one of two values, like:
the boolean keyword
and can only take YES / NO
ON / OFF
the values true or TRUE / FALSE
false:
For this, Java has a boolean data type,
which can store true or false values.
Java
Comparison Operators
Java Booleans
Comparison Operators are Operator Name Example
used to compare two == Equal to x == y
values (or variables).
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
The
The == Operators
Not equal to
Returns True, Example 1 _ not equal to (==) Operator
because x is not
equal to y.
The
The != Operators
Not equal to
Returns True, Example 2 _ not equal to (!=) Operator
because x is not
equal to y.
Java
Boolean Values
Java Booleans
A boolean type is Activity 1
declared with
the boolean keyword boolean isJavaFun = true; boolean
isFishTasty = false;
and can only take
the values true or System.out.println(isJavaFun);
false: // Outputs true
System.out.println(isFishTasty);
// Outputs false
Java
Boolean Expression
Java Booleans
This is useful to build A Boolean expression returns a Boolean value:
logic, and find answers. true or false.
For example, you can use
a comparison operator, Activity 2
such as the greater
than (>) operator, to find int x = 10; int y = 9;
out if an expression (or a System.out.println(x > y);
variable) is true or false:
// returns true, because 10 is higher
than 9
Java
Boolean Expression
Java Booleans
This is useful to build A Boolean expression returns a Boolean value:
logic, and find answers. true or false.
For example, you can use
a comparison operator, Activity 2.1
such as the greater
than (>) operator, to find
out if an expression (or a System.out.println(10 > 9);
variable) is true or false:
// returns true, because 10 is higher
than 9
Java
Boolean Expression
Java Booleans
A Boolean expression returns a Boolean value:
In the examples below, we true or false.
use the equal to (==)
operator to evaluate an Activity 4
expression:
int x = 10;
int y = 12;
System.out.println(x == 10);
System.out.println(x == y );
// returns true, because the value of x
is equal to 10
Java
Boolean Expression
Java Booleans
A Boolean expression returns a Boolean value:
In the examples below, we true or false.
use the equal to (==)
operator to evaluate an Activity 3.1
expression:
System.out.println(10 == 15);
System.out.println(10 == 10);
System.out.println(100 == 10*10);
// 1st line returns false, because 10 is
not equal to 15
Java
Real Life Example
Java Booleans
A Boolean expression returns a Boolean value:
Let's think of a "real life true or false.
example" where we need
to find out if a person is Activity 4
old enough to vote.
In the example below,
we use the >= comparison operator to find out
if the age (25) is
greater than OR qual to
the voting age limit, which is set to 18:
Java
Real Life Example
Java Booleans
A Boolean expression returns a Boolean value:
Let's think of a "real life true or false.
example" where we need
to find out if a person is Example 6
old enough to vote.
int myAge = 25;
int votingAge = 18;
System.out.println(myAge >= votingAge);