If you do the following:
public class foo { public static void main(String [] args) { Integer i1 = new Integer(11); Integer i2 = new Integer(11); boolean t1 = (i1 == i2); boolean t2 = i1.equals(i2); System.out.println("t1 = " + t1 + " and t2 = " + t2); } }
You get:
t1 = false and t2 = true
What is going on here?
== of two objects tests if they refer to exactly the same object. You could
get this to happen by doing:
i2 = i1;
It does not test if what the object represents is the same. Here i1
and i2 are both Integers storing the integer value, 11. If you want to compare
values you use the equals method. This is why t1 is false and t2 is true.
When you create a class, you inherit an equals method. If no subclass changes the meaning from the Object class (from which all classes are derived) then it compares the reference just like ==. However, it is good practice to provide your own equals method for a class if you can compare two instances of a class. The Integer class does this so equals compares the ints the object refers to. This is what people expect from the equals method.
What does this mean for the assignment?
When you look for items on a list, you usually prefer to see if what the object represents (not the reference) is the same. Thus, it is usually preferable to use the equals method. If you haven't done this yet then use this way. However, a number of people were confused about what to do and used ==. If you already did this then it is ok for this assignment. You will get credit either way as long as it works correctly for what you did.