AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • What are Booleans?
  • Meet George Boole
  • Working with Booleans

Was this helpful?

  1. Basic Java

Booleans

PreviousCastingNextLogical Operators

Last updated 5 years ago

Was this helpful?

How do we write code that tells us whether a user is logged in to our program? Booleans are the solution to these questions.

What are Booleans?

A boolean refers to a value that is true or false. Those are the only values of a boolean expression, and these are useful if we want to check if something is true or false.

Meet George Boole

Let's meet the fellow behind the name, "booleans," George Boole. Mr. Boole was an English-born mathematician, philosopher, and logician. He introduced boolean algebra in The Mathematical Analysis of Logic (1847) and Investigation of the Laws of Thought (1854). Many consider him to be one of the founders of Computer Science.

Working with Booleans

How about an example? Let's create a variable and set it equal to true. Then, we'll print the variable.

We first want to declare the variable, loggedIn, and set it to true or false.

boolean loggedIn = false;

We can similarly set the variable to true instead:

boolean loggedIn = true;

You can imagine a boolean variable as a box that can hold only the values true or false. The box below shows the current value of loggedIn:

Notice that we do not need to have quotations around true or false.

The full example looks like:

// In this program we declare a boolean
// value to represent whether the user
// is logged in, and then print it out.
public class LoggedIn extends ConsoleProgram
{
    public void run()
    {
        boolean loggedIn = false;
        System.out.println("User logged in?: " + loggedIn);
    }
}

Remember, use booleans if you want to set a variable to true or false!

George Boole
CodeHS