2.2 - Boolean Expressions

mg8mer

Introduction

Welcome to the FiveHive article for topic 2.2 of AP CSA! 

In this article, we will cover the learning objective of 2.2 as shown in the AP® Computer Science A Course and Exam Description 2025 

2.5.A - Develop code to represent compound Boolean expressions and determine the result of these expressions.

This is going to be a relatively short, but important article introducing boolean expressions, which serve as the foundation for selection (covered briefly in 2.1) and by association if statements in Java (more on them in 2.3 and 2.4).

Anyways, let’s not waste anymore time and just jump straight in!

What are boolean expressions?

A boolean expression is a statement that either evaluates to true or to false (hence why it’s called a boolean expression - it has TWO outcomes, both of which are called boolean values).

Relational Operators

In Java, a boolean expression is one that involves relational operators. Or in non-fancy, non-CollegeBoard speak, symbols that suggest some kind of comparison between two values (ints, doubles, objects, and so on). 

The relational operators that are shown in the CED and will 100% show up in some way on your AP exam are:

  • Equality (determining whether two values are the same)
    • == (evaluates to true if two values are equal)
    • != (the opposite of ==)
      • Think of it like this: if comparing two things using “==” yields true, then using “!=” flips that result to false. If comparing two things using “==” yields false, then using “!=” flips that result to true.
  • Comparative (ONLY VALID FOR NUMERICAL VALUES) → determines how two values are related (i.e if one number is greater than the other)
    • > (greater than)
    • < (less than)
    • <= (less than or equal to)
      • NOT
    • >= (greater than or equal to)
      • NOT

More on Equality Operators

That question was probably really easy for you. 

Now try this one. What does this program output? Assume Car is a class that has been written and implemented in some other file and has a constructor that takes two ints as parameters:

Many of you probably got that wrong. If you didn’t, good job! For those that got it wrong, what probably threw you off is how both Car variables are instantiated by inputting the same values into the constructor, and thus comparing them using == must evaluate to true. 

However, comparing objects is different to comparing primitive values (ints, doubles, etc). 

A Helpful Analogy

Imagine for a second that variables in Java are like boxes. Just like variables, boxes store something inside them (whether it be a PS5, an orange, a cat (maybe)*). In this analogy, let’s have the box be named after whatever we call our variable.

*Please tell me that someone got this stupid reference

For primitive variables, the box will simply store the value of the variable directly. Let this box be stored in a place called the primary address.

For example:

int bro = 340259;

The box called bro will directly store the int value of 340259 like so:

For object variables, they like to be quirky and different. Instead of storing what was inputted into the constructor directly in the box that is in the primary address, in the box is simply a metaphorical slip of paper that basically just says “haha get trolled, you’ll find what you put into the constructor in another box that’s at this location [INSERT MEMORY ADDRESS HERE]”.

If you think about it, it makes sense that objects are called reference types. Their boxes at the primary address store a reference to another box.

For every object you instantiate, the location where the actual constructor-passed values are stored is different, and when I say every, I mean every. Even if you instantiate two objects of the same class and pass the same values into their respective constructors, their respective boxes at their primary addresses will store different addresses to different boxes. I’ll get to why that’s important in a second.

Yes, this is kind of complicated. Don’t worry, let me illustrate this, I gotchu:

Car ferrariOne = new Car(90, 871);
Car ferrariTwo = new Car(90, 871);

Here are the two Car variables y’all saw in the previous question.

The box called ferrariOne at the primary address will store an address to another box that stores the constructor-passed values 90 and 871. Let that address be 308 Negra Arroyo Lane, (Albuquerque, New Mexico).

The box called ferrariTwo at the primary address will store another, different address to another box that stores the constructor-passed values 90 and 871. Let that address be 123 Sesame Street.

 

I’m sure you’re now asking, “how is this relevant to why ferrariOne == ferrariTwo evaluates to false?”

Well, when we use == or !=, they are actually comparing the values stored in the boxes that are in the primary address. For primitive variables, this is not a problem of course, as their values are directly stored in that box. However, for object variables (or reference types), every object has a different address stored in the box that is in the primary address, even if both objects are of the same class and have the same values passed into their respective constructors.

Thus, this is how ferrariOne == ferrariTwo evaluates to false.

Practice

Let’s practice what we just learned through two questions!