Lesson 9 +10 XP

Booleans

Java Booleans

A boolean is a data type that can only hold two values: true or false. Booleans are great for making decisions in your code.

Declaring booleans

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun);
System.out.println(isFishTasty);

Boolean expressions

A boolean expression compares values and returns true or false:

int x = 10;
int y = 9;
System.out.println(x > y); // true, because 10 is higher than 9

Comparison operators

OperatorMeaning
>greater than
<less than
==equal to
!=not equal to
>=greater than or equal
<=less than or equal

Using booleans in conditions

int age = 25;
int votingAge = 18;
System.out.println(age >= votingAge); // true

TL;DR

  • Booleans hold only true or false.
  • Comparison operators create boolean values.
  • Use booleans to control decisions like if statements.