codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

12 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Booleans
☕Java Programming

Java Booleans

Updated 2026-05-12
30 min read

Java Booleans

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.

Introduction

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:

  • What boolean values are and how to declare them.
  • How to use boolean expressions to evaluate conditions.
  • Practical examples demonstrating condition checking in Java.

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.

Core Content

What Are Boolean Values?

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.

Declaring Boolean Variables

To declare a boolean variable in Java, use the boolean keyword followed by the variable name and an optional initial value.

BooleanDeclaration.java
1public class BooleanDeclaration {
2 public static void main(String[] args) {
3 boolean isTrue = true;
4 boolean isFalse = false;
5
6 System.out.println("isTrue: " + isTrue);
7 System.out.println("isFalse: " + isFalse);
8 };
9}
Output
isTrue: true
isFalse: false

Boolean Expressions

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.

Common Comparison Operators

Here are some common comparison operators used in Java:

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example: Using Comparison Operators

Let's look at an example where we use comparison operators to evaluate boolean expressions.

ComparisonOperators.java
1public class ComparisonOperators {
2 public static void main(String[] args) {
3 int a = 10;
4 int b = 20;
5
6 boolean isEqual = (a == b);
7 boolean isNotEqual = (a != b);
8 boolean isGreater = (a > b);
9
10 System.out.println("isEqual: " + isEqual); // false
11 System.out.println("isNotEqual: " + isNotEqual); // true
12 System.out.println("isGreater: " + isGreater); // false
13 }
14}
Output
isEqual: false
isNotEqual: true
isGreater: false

Condition Checking

Condition checking involves evaluating boolean expressions to determine the flow of a program. This is typically done using conditional statements like if.

Example: Basic If Statement

Here's an example demonstrating how to use an if statement to check a condition.

BasicIfStatement.java
1public class BasicIfStatement {
2 public static void main(String[] args) {
3 int number = 10;
4
5 if (number > 5) {
6 System.out.println("Number is greater than 5");
7 }
8 }
9}
Output
Number is greater than 5

Example: If-Else Statement

An if-else statement allows you to execute different blocks of code based on whether a condition is true or false.

IfElseStatement.java
1public class IfElseStatement {
2 public static void main(String[] args) {
3 int number = 10;
4
5 if (number > 20) {
6 System.out.println("Number is greater than 20");
7 } else {
8 System.out.println("Number is not greater than 20");
9 }
10 }
11}
Output
Number is not greater than 20

Logical Operators

Logical operators are used to combine multiple boolean expressions into a single expression. The three main logical operators in Java are:

  • && (AND)
  • \|\| (OR)
  • ! (NOT)

Example: Using Logical Operators

Here's an example demonstrating how to use logical operators.

LogicalOperators.java
1public class LogicalOperators {
2 public static void main(String[] args) {
3 int a = 10;
4 int b = 20;
5
6 boolean andResult = (a > 5 && b < 30);
7 boolean orResult = (a < 5 || b > 30);
8 boolean notResult = !(a == b);
9
10 System.out.println("andResult: " + andResult); // true
11 System.out.println("orResult: " + orResult); // false
12 System.out.println("notResult: " + notResult); // true
13 }
14}
Output
andResult: true
orResult: false
notResult: true

Practical Example

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.

EvenOddChecker.java
1public class EvenOddChecker {
2 public static void main(String[] args) {
3 int number = 23;
4
5 if (number % 2 == 0) {
6 System.out.println(number + " is even");
7 } else {
8 System.out.println(number + " is odd");
9 }
10 }
11}
Output
23 is odd

Summary

In this tutorial, we covered:

  • What boolean values are and how to declare them.
  • How to use comparison operators to evaluate conditions.
  • Practical examples demonstrating condition checking in Java.
  • Logical operators for combining multiple boolean expressions.

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.

What's Next?

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!


PreviousJava MathNext Java If...Else

Recommended Gear

Java MathJava If...Else