AP CSA 2.6 Debugging: Equal Objects, Different Boxes

Unit 2: Selection and Iteration · Lesson 2.6 · Debugging

Equal Objects, Different Boxes

Comparing Boolean Expressions. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

Two Strings that read the same on screen are not necessarily the same object, and == asks the object question, not the reading question. This is the single most common runtime surprise in the whole course, and it never produces an error message.

What is wrong with it

  1. Run the program with the two words typed the same. The first line prints false anyway. The comparison uses == on two Strings, which compares whether they are the same object in memory, not whether they contain the same characters. Use the method that compares contents.
  2. The third line is supposed to print whether the two words are NOT both non-empty. The not was distributed across the and incorrectly. Fix it so it matches the sentence.
  3. The second line, which compares the lengths, is already correct. Lengths are ints, and == is right for ints.

The program reads

Two words separated by whitespace.

The program should print

Three lines of true or false: whether the words are the same text, whether their lengths are equal, and whether it is not the case that both words have length greater than zero.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
cat cat
Example 1 output
true
true
false
Example 2 input
apple pear
Example 2 output
false
false
false

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

new String(first) deliberately builds a second object holding the same characters. == compares the two references, and they point at different objects, so it is false no matter what the words say. equals compares character by character.

Hint 2

You may have seen == appear to work on Strings in other programs. That is the string pool reusing one object for identical literals, and it is an optimization, not a guarantee. The moment a String comes from input or from new, the trick stops working. Never rely on it.

Hint 3

For the third line, !(P && Q) is false only when both P and Q are true. !P && !Q is true only when both are false. Those are different in the two mixed cases: one empty word and one not.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]