4 - Topic_Java Bolean
4 - Topic_Java Bolean
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
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
2
Lecture Points
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
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
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.
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)
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:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
Exercise (2)
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 :
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All
rights reserved.
References
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.