Java equals
Compare String text with equals so matching words print true. In Yard.java, build String a = "red"; and String b = "red";, then print a.equals(b). Add String c = "blue"; and print a.equals(c). Compile with javac Yard.java and run java Yard. The screen should show true, then false. A second demo builds two new String("red") objects so == can print false while equals still prints true. Leave debugging for the next page — this lesson is content match versus object identity.
Match text with equals
The polymorphism page ran different horn bodies through Machine-typed variables. This page compares text. Open Yard.java. Keep the public class name Yard. Delete leftover Machine / Truck / Car demos if they bury the string story, leftover abstract fills, leftover path demos, and any keyboard Scanner. The program must not wait for typing. No import line is required for these demos.
Inside Yard.main, build three strings:
String a = "red";
String b = "red";
String c = "blue";
Print the comparisons:
System.out.println(a.equals(b));
System.out.println(a.equals(c));
The method equals looks at the characters. Same text returns true. Different text returns false.
public class Yard {
public static void main(String[] args) {
String a = "red";
String b = "red";
String c = "blue";
System.out.println(a.equals(b));
System.out.println(a.equals(c));
}
}
From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard.
The screen shows true, then false. Walk the run once. Java starts at Yard.main. a.equals(b) compares red with red and prints true. a.equals(c) compares red with blue and prints false. You called a method on a and passed the other string as the argument.
If you still see tone / honk / beep from a polymorphism file, switch to the equals teaching file and run java Yard again. If you print the strings themselves instead of the equals results, you will see red and blue on the screen — wrap a.equals(b) inside println so the boolean appears.
Say the rule out loud while you watch the screen: equals asks whether the text matches character by character. Do not invent a debugger window on this page. Debugging waits for the next lesson.
Contrast == with equals on new String
A second program builds two separate String objects with new. Both hold the text red. The program prints a == b and then a.equals(b).
public class Yard {
public static void main(String[] args) {
String a = new String("red");
String b = new String("red");
System.out.println(a == b);
System.out.println(a.equals(b));
}
}
Compile with javac Yard.java, then run java Yard. The screen shows false, then true. The == check asks whether a and b are the exact same object in memory. Two new String("red") calls build two objects, so == prints false. The equals check still looks at the characters, so it prints true.
Read the two lines in main once more. First line: identity with ==. Second line: content with equals. Keep both prints for this teaching shape so the contrast stays on one screen. If you only print ==, you miss the content match this page also teaches.
On beginner string literals like "red" assigned without new, Java may reuse the same pooled object, so == can sometimes look true by accident. This second demo uses new String("red") on purpose so the identity check stays clearly false while content stays true. Prefer equals when you care about the text.
If leftover polymorphism demos still sit nearby, keep them as history. For this lesson run the equals teaching file so the screen shows booleans, not horn words. If javac complains about duplicate public classes, keep only Yard public.
Compared with polymorphism calls
On the polymorphism page, one method name ran different bodies through parent-typed variables. On this page, one method name — equals — answers whether two strings hold the same text, and == answers whether they are the same object. You still compile Yard.java and run java Yard. The new tokens are equals, the boolean true / false prints, and the == contrast with new String.
If a leftover Machine t = new Truck() file is still open, you will see horn calls. Switch to the equals file so main builds strings and prints comparison results. Keep debugging-tool demos off this page. Keep keyboard input off this page.
What this leaves for debugging
This page compares string content and shows why == is the wrong default for text. The next page shows debugging, where you find why a program misbehaves with careful reads of the screen and the source. Stay with equals and == here. Do not require a full debugger walkthrough yet.
When you later debug a wrong branch, a failed equals check is a common cause. Treat this page as the comparison half of that neighboring story. Finish both Yard demos until true / false and false / true feel automatic before you open the debugging lesson.
Before debugging
You can call a.equals(b) on matching red text and see true, then see false for blue. You can build two new String("red") objects and see == print false while equals prints true. Compile Yard.java and run java Yard for each kept demo in turn. Content match and object identity stayed separate. The next page is debugging, where you hunt mistakes with the screen and the source side by side.
Keep the three-string equals demo for a clear content check. Keep the new String demo for a clear == contrast. Keep debugging files off this page. Those three habits are the checkpoint before you move on. When equals prints true for matching text — and when == prints false for two new objects with that text — this lesson did its job and you are ready for the next topic about debugging.
One last pass through the folder: one public Yard, string comparisons in main, no keyboard wait, no forced debugger. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still prints the booleans you expect, the comparison story is solid and the course can move to debugging on the next page.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top