Object Superclass

AP Computer Science A· difficulty 3/5

public class Tag {
    private String label;
    public Tag(String l) { label = l; }
}
// ...
Tag t1 = new Tag("X");
Tag t2 = new Tag("X");
System.out.println(t1.equals(t2));

What is printed?

  • A

    false

    check_circle
  • B

    true

  • C

    X

  • D

    Compile-time error

Explanation

Without overriding equals, Object's equals compares references; t1 and t2 are different objects, so it returns false.

Want 10 more like this — adaptive to your weak spots?

Related questions