Java Booleans

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 12 / 50 · English

Java Booleans

Make a decision explicit.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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 / கோடின் விளக்கம்

CodeMeaning
booleanThe data type. A boolean can only hold true or false — nothing else.
isLoggedInThe variable name. Boolean names usually start with 'is', 'has', or 'can' to make them read like a yes/no question.
=Assignment operator.
trueThe 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.

Java reference fragmentJAVA
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);     // false
03 / Read and predict

A 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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.