Java Booleans
Make a decision explicit.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Booleans.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
A boolean has one of two values: true or false. Build conditions with comparisons, combine them with && and ||, and read the whole expression as a question. Short-circuit operators can skip evaluating the right-hand side when the left side determines the result.
Explore the details
Java Booleans
A boolean is the simplest data type in Java — it can only ever be true or false. Booleans are the foundation of every decision your program makes.
- Is the user logged in? →
boolean isLoggedIn = true; - Is the light on? →
boolean isLightOn = false; - Is it raining? →
boolean isRaining = true;
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
boolean | The data type. A boolean can only hold true or false — nothing else. |
isLoggedIn | The variable name. Boolean names usually start with 'is', 'has', or 'can' to make them read like a yes/no question. |
= | Assignment operator. |
true | The boolean value. Only two options exist: true or false (always lowercase in Java). |
; | End of statement. |
Boolean Expressions
In practice, booleans are rarely written as literals. They are almost always the result of an expression — a comparison that Java evaluates to true or false:
Real-Life Example: Voting Age
Booleans shine when you use them to make decisions. Here we check if a person is old enough to vote (age ≥ 18) and print a different message depending on the result:
Combining Booleans
You can combine boolean expressions using logical operators to build more complex conditions:
Booleans are the foundation of all decision-making in Java. Every if statement, every loop condition, every comparison — they all produce or consume a boolean value. You will learn how to use them in if...else in the next chapter.
int age = 20;
boolean isAdult = (age >= 18); // true
boolean hasTicket = true;
// Logical operators
boolean canEnter = isAdult && hasTicket; // true (both true)
boolean eitherOr = isAdult || hasTicket; // true (at least one)
boolean notKid = !isAdult; // false (inverts true)
System.out.println(isAdult); // true
System.out.println(canEnter); // true
System.out.println(notKid); // falseA complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int age = 20;
boolean hasTicket = true;
boolean canEnter = age >= 18 && hasTicket;
System.out.println(canEnter);
System.out.println(!hasTicket);
}
}Predict first. Then reveal the expected output / வெளியீடு
true false
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Set hasTicket to false and predict both results before running.
Reveal the answer and reasoning / விடை
canEnter becomes false. !hasTicket becomes true.
One mistake to avoid: && requires both conditions to be true; || requires at least one. Check whether your actual requirement says both or either.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.