Relational operator in AP Computer Science A

In AP Computer Science A, a relational operator compares two values and evaluates to a Boolean (true or false). The six relational operators are ==, !=, <, >, <=, and >=. With primitives they compare actual values; with reference types, == and != compare object references, not contents.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is relational operator?

A relational operator is the part of Java that asks a yes-or-no question about two values. x < 10 asks "is x less than 10?" and the answer is always a Boolean, either true or false. That's the whole job. Per the CED (EK 2.2.A.3), any expression built with a relational operator evaluates to a Boolean value, which is exactly what if statements and loops need to make decisions.

The six operators split into two groups. The equality pair, == and !=, checks whether two values are the same and works on any type (EK 2.2.A.1). The ordering group, <, >, <=, and >=, compares numeric values to see how they relate (EK 2.2.A.2). One huge catch lives in that first group. With primitive types like int and double, == compares the actual values. With reference types like String, == compares the references, meaning it asks "are these literally the same object in memory?" rather than "do these hold the same content?" That single distinction is one of the most-tested traps in the course.

Why relational operator matters in AP® Computer Science A

Relational operators live in Topic 2.2 (Boolean Expressions) inside Unit 2: Selection and Iteration, and they directly support learning objective 2.2.A, which says you should be able to develop code with relational operators and determine what the expressions evaluate to. Notice that's two skills. You write the comparison, and you trace it to a definite true or false.

The reason this tiny topic punches above its weight is that everything in Unit 2 sits on top of it. Every if statement, every while loop, every for loop condition is a Boolean expression, and relational operators are how most of those Booleans get made. If you can't confidently evaluate x >= 10 or spot why s1 == s2 is suspicious for Strings, the rest of selection and iteration gets shaky fast.

How relational operator connects across the course

== (equality operator) and object references (Unit 2)

The == operator is the relational operator with a split personality. On primitives it compares values, so 5 == 5 is true. On objects it compares references, so two different String objects with identical text can make s1 == s2 false. That's why .equals() exists for content comparison.

Logical operators and compound Boolean expressions (Unit 2)

Relational operators produce Booleans, and logical operators (&&, ||, !) combine them. An expression like (x >= 10) != (y <= 5) chains two relational results together. Think of relational operators as making the individual true/false bricks and logical operators as the mortar.

Selection statements (Unit 2)

An if statement is useless without a Boolean condition to test, and relational operators are the most common way to build one. if (currentTemp <= averageTemp - 10.0) only branches correctly if you can evaluate that comparison correctly first.

Loop conditions in iteration (Unit 2)

Every while and for loop keeps running as long as its Boolean condition is true, and that condition is almost always a relational expression like i < arr.length. Off-by-one errors usually come down to choosing < when you needed <=, or vice versa.

Is relational operator on the AP® Computer Science A exam?

Relational operators show up constantly in multiple-choice questions, usually inside a code-tracing task. A typical stem gives you a short segment like boolean isMatch = (x >= 10) != (y <= 5); and asks which initial values make it true, or shows a method like boolean p = x >= y; boolean q = x <= y; return p && q; and asks you to describe its behavior (that one returns true only when x equals y, a classic pattern). Comparisons on double values appear too, like checking whether currentTemp <= averageTemp - 10.0.

The other reliable test angle is the String trap. A question may pit s1 != s2 against s1.equals(s2) and ask what a method really accomplishes, which checks whether you know == and != compare references for objects. No released FRQ centers on relational operators by name, but nearly every FRQ you write will use them inside if conditions and loop bounds, so a wrong operator choice quietly costs points on logic.

Relational operator vs logical operator

Relational operators (==, !=, <, >, <=, >=) compare two values and produce a Boolean. Logical operators (&&, ||, !) take Booleans as input and combine or flip them. In (x >= 10) && (y < 5), the relational operators do the comparing and && does the combining. If the operands are numbers, you're looking at a relational operator. If the operands are already true/false values, it's a logical operator.

Key things to remember about relational operator

  • The six relational operators in AP CSA are ==, !=, <, >, <=, and >=, and every expression built with them evaluates to a Boolean value (true or false).

  • With primitive types like int and double, == and != compare the actual values stored in the variables.

  • With reference types like String, == and != compare object references (whether two variables point to the same object), so use .equals() to compare contents.

  • The ordering operators <, >, <=, and >= only work on numeric values, and choosing between < and <= is where most off-by-one loop errors come from.

  • = assigns a value and == compares values; mixing them up is a classic error worth checking for in any code-tracing question.

  • x >= y combined with x <= y using && is only true when x equals y, a pattern the exam likes to disguise inside mystery methods.

Frequently asked questions about relational operator

What is a relational operator in AP Computer Science A?

It's an operator that compares two values and evaluates to a Boolean result. The six relational operators in the AP CSA course are ==, !=, <, >, <=, and >=, covered in Topic 2.2 (Boolean Expressions).

Does == work for comparing Strings in Java?

Not the way you'd expect. For Strings (and all reference types), == compares object references, meaning it checks whether two variables point to the same object, not whether the text matches. Use .equals() to compare String contents; the exam tests this trap directly.

What's the difference between a relational operator and a logical operator?

Relational operators (==, !=, <, >, <=, >=) compare two values and produce a Boolean. Logical operators (&&, ||, !) take Booleans and combine them. In (x > 0) && (y > 0), the > signs are relational and the && is logical.

Is = the same as == in Java?

No. A single = is the assignment operator (it stores a value in a variable), while == is the equality operator (it compares two values and returns a Boolean). Writing if (x = 5) instead of if (x == 5) won't compile for an int, and it's a bug the exam expects you to catch.

What does a relational expression evaluate to?

Always a Boolean, either true or false (EK 2.2.A.3). That's why relational expressions slot directly into if statement conditions and while/for loop conditions, which require a Boolean to decide what happens next.