In the world of programming, making decisions is a fundamental task. Whether you're building a simple calculator or a complex web application, understanding how to check conditions and make decisions based on them is crucial. In Java, this is achieved through boolean values and expressions.
Boolean logic forms the backbone of decision-making in programming. A boolean value can only be true or false, representing two states: yes or no, on or off, true or false. This binary nature makes booleans incredibly powerful for controlling the flow of a program based on different conditions.
In this tutorial, we'll explore:
By the end of this tutorial, you'll be able to write programs that make decisions based on boolean logic. This knowledge is essential for moving forward into more complex topics like loops and conditional statements.
A boolean value in Java can only be true or false. These values are used to represent the outcome of a condition or comparison. For example, checking if two numbers are equal results in a boolean value.
To declare a boolean variable in Java, use the boolean keyword followed by the variable name and an optional initial value.
1public class BooleanDeclaration {2public static void main(String[] args) {3boolean isTrue = true;4boolean isFalse = false;56System.out.println("isTrue: " + isTrue);7System.out.println("isFalse: " + isFalse);8};9}
isTrue: true isFalse: false
A boolean expression is an expression that evaluates to a boolean value (true or false). These expressions are often used in conditional statements like if, else, and loops.
Here are some common comparison operators used in Java:
| Operator | Description |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Let's look at an example where we use comparison operators to evaluate boolean expressions.
1public class ComparisonOperators {2public static void main(String[] args) {3int a = 10;4int b = 20;56boolean isEqual = (a == b);7boolean isNotEqual = (a != b);8boolean isGreater = (a > b);910System.out.println("isEqual: " + isEqual); // false11System.out.println("isNotEqual: " + isNotEqual); // true12System.out.println("isGreater: " + isGreater); // false13}14}
isEqual: false isNotEqual: true isGreater: false
Condition checking involves evaluating boolean expressions to determine the flow of a program. This is typically done using conditional statements like if.
Here's an example demonstrating how to use an if statement to check a condition.
1public class BasicIfStatement {2public static void main(String[] args) {3int number = 10;45if (number > 5) {6System.out.println("Number is greater than 5");7}8}9}
Number is greater than 5
An if-else statement allows you to execute different blocks of code based on whether a condition is true or false.
1public class IfElseStatement {2public static void main(String[] args) {3int number = 10;45if (number > 20) {6System.out.println("Number is greater than 20");7} else {8System.out.println("Number is not greater than 20");9}10}11}
Number is not greater than 20
Logical operators are used to combine multiple boolean expressions into a single expression. The three main logical operators in Java are:
&& (AND)\|\| (OR)! (NOT)Here's an example demonstrating how to use logical operators.
1public class LogicalOperators {2public static void main(String[] args) {3int a = 10;4int b = 20;56boolean andResult = (a > 5 && b < 30);7boolean orResult = (a < 5 || b > 30);8boolean notResult = !(a == b);910System.out.println("andResult: " + andResult); // true11System.out.println("orResult: " + orResult); // false12System.out.println("notResult: " + notResult); // true13}14}
andResult: true orResult: false notResult: true
Let's create a practical example that combines boolean values, expressions, and condition checking. We'll write a program that checks if a given number is even or odd.
1public class EvenOddChecker {2public static void main(String[] args) {3int number = 23;45if (number % 2 == 0) {6System.out.println(number + " is even");7} else {8System.out.println(number + " is odd");9}10}11}
23 is odd
In this tutorial, we covered:
Understanding booleans and condition checking is essential for controlling the flow of a program. This knowledge will serve as a foundation for more advanced topics like loops and conditional statements.
Now that you have a solid understanding of booleans, the next step is to learn how to use them in conditional statements like if, else, and switch. We'll explore these concepts in detail in the next tutorial: Java If...Else.
By mastering condition checking, you'll be well on your way to writing more complex and dynamic Java programs. Stay tuned!