Preview

21 - Comparing Objects

 1. The == operator can be used to check if two object references __________________.
if (objRef1 == objRef2) {
    // The two object references point to the same object
}

  do not equal each other

  point to the same object.

  are exactly equal

  have the same string value

 2. The implementor decides which values must be equal to consider two objects to be equal. For example in the below class, the name and the address must be equal but not the __________.
public class Customer {
    private String name;
    private String address;
    private String description;
    // ...
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        } else if (obj == null) {
            return false;
        } else if (obj instanceof Customer) {
            Customer cust = (Customer) obj;
            if ((cust.getName() == null && name == null) ||
                (cust.getName().equals(name) && ((cust.getAddress() == null && address == null)
                || cust.getAddress().equals(address))) {
                return true;
            }
        }
        return false;
    }

}

  description

  string

  null operator

  value

 3. After the equals() method is overriden, two objects from the same class can be compared like this:
Customer cust1 = new Customer();
Customer cust2 = new Customer();
//...
if (cust1.equals(cust2)) {
    // Two Customers are equal, by name and address
}

  TRUE

  FALSE

 4. The above println statement (the entire line) is equivalent to what?
Point p1 = new Point();
...
System.out.println(p1);

  System.out.println(toString(p1));

  System.out.println(p1.toString());

  System.out.println(p1.string());

  p1.toString();

 5. What is written to the monitor by the following section of code:
String strA = new String("R ");
String strB = new String("A"); 

strA = strB;
if ( strA == strB )
  System.out.print("Two copies of a reference.");
else
  System.out.print("Two different references.");

  Two different references

  Nothing is printed

  Two copies of a reference

  Two copies of a reference

 6. How many objects have been created? After the last statement has executed, how many objects are now accessible (don't count garbage)?
String strA = new String("R ");
String strB = new String("A "); 

strA = strB;

  created: 0 now accessible: 0

   created: 2 now accessible: 1

  created: 1 now accessible: 1

   created: 2 now accessible: 2

 7. How many objects (total) are created? After the last statement has executed, how many objects are now accessible (don't count garbage)?
String strA = new String("R ");
String strB = strA;
String strC = strA;
String strD = strA;
String strE = strA;

  This code is incorrect

   created: 5 now accessible: 1

  created: 1 now accessible: 1

  created: 5 now accessible: 5

 8. When comparing strings with one another, use the equals method ? not the double equal sign.

  FALSE

  TRUE

 9. Object value equality: when two object references point to the same object.

  FALSE

  TRUE

 10. Object reference equality: when two separate objects happen to have the same values/state.

  FALSE

  TRUE