Chapter 3 Java Boolean
Modified from:
-W3Schools.com
-Introduction to Java Programming, Liang, 10 th Edition
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
1
Introduction to the lecture
In this lecture will talk how deal with the
boolean types and Operators, Boolean Values, Boolean
Expression, and Relational Operators in JAVA
programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Lecture Points
The boolean Type and Operators
Boolean Values
Boolean Expression
Relational Operators
Some Exercise
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
3
The boolean Type and Operators
Very often, in programming, you will need a
data type that can only have one of two values,
like:
YES / NO
ON / OFF
TRUE / FALSE
For this, Java has a boolean data type, which
can take the values true or false.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
4
Boolean Values
A boolean type is declared with the boolean keyword
and can only take the values true or false.
Example
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false
However, it is more common to return boolean values
from boolean expressions, for conditional testing
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
5
Boolean Expression
A Boolean expression is a Java expression that returns a
Boolean value: true or false.
You can use a comparison operator, such as the greater
than (>) operator to find out if an expression (or a variable) is
true
Java provides six comparison operators (also known as
relational operators) that can be used to compare two values.
The result of the comparison is a Boolean value: true or false.
boolean b = (1 > 2);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
6
Relational Operators
Java Mathematics Name Example Result
Operator Symbol (radius is 5)
< < less than radius < 0 false
<= ≤ less than or equal to radius <= 0 false
> > greater than radius > 0 true
>= ≥ greater than or equal to radius >= 0 true
== = equal to radius == 0 false
!= ≠ not equal to radius != 0 true
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
7
Boolean Expression Example (1)
int x = 10;
int y = 9;
System.out.println(x > y); // returns true, because 10 is higher than 9
Or even easier:
Example
System.out.println(10 > 9); // returns true, because 10 is higher than
9
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
8
Boolean Expression Example (2)
In the examples below, we use the equal to (==) operator to
evaluate an expression:
Example
int x = 10;
System.out.println(x == 10); // returns true, because the value of
x is equal to 10
Example
System.out.println(10 == 15); // returns false, because 10 is not equal
to 15
Note: The Boolean value of an expression is the basis for all Java
comparisons and conditions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
9
Boolean Expression Example (3)
In the examples below, we use the equal to (==) operator to
evaluate an expression:
Example
int x = 10;
System.out.println(x == 10); // returns true, because the value of
x is equal to 10
Example
System.out.println(10 == 15); // returns false, because 10 is not equal
to 15
Note: The Boolean value of an expression is the basis for all Java
comparisons and conditions.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
10
Exercise (1)
Fill in the missing parts to print the values true and false:
__________ isJavaFun = true;
__________ isFishTasty = false;
System.out.println(isJavaFun);
System.out.println(isFishTasty);
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Exercise (2)
Fill in the missing parts to print the value true :
int x = 10;
int y = 9;
System.out.println(___ ___ ___ );
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Summary of lecture
In this lecture, learned about some elements in JAVA
programming that are included :
The boolean Type and Operators
Boolean Values
Boolean Expression
Relational Operators
Some Exercise
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
References
Y. Daniel Liang, 2019, Intro to Java Programming,
Comprehensive Version, Student Value Edition 12th Edition.
Pearson, ISBN-10 : 0136520154 ISBN-13: 978-0136520153.
Introduction to Java,
https://www.w3schools.com/java/java_intro.asp, Last Updated
2024, Last Accessed March 2024.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.