Suppose I have 开发者_如何学Goan MyObject
class, and it's stored in 2 different ArrayList
. If get the same object from the 2 ArrayList, how would I compare them to check if they're identical?
Would I do instance1 == instance2
or instance1.equals(instance2)
? I'm pretty sure the == operator looks at the pointer location, but if I get instance1 from array1 and instance2 from array2, would they return different pointer locations or the same?
Same thing with Hashtable: Does it store a pointer location as the key or the actual object? If I have 2 objects with identical content but are actually different objects, would it still work?
Thanks
You need to differentiate between objects and references.
Two different lists (or in general, any expressions) can be equal references to the same object. For example:
MyObject x = new MyObject();
MyObject y = x;
Here x
and y
are separate variables but they both have values referring to the same object. That "referring to the same object" is what ==
will compare.
If you use equals
then (assuming you've overridden equals
in MyObject
) two different objects can still be equal.
For example:
Integer x = new Integer(1);
Integer y = new Integer(1);
System.out.println(x == y); // False
System.out.println(x.equals(y)); // True
Here the values of x
and y
are references to different objects, but those objects are considered to be equal.
So, does your MyClass
class have a notion of object equality beyond just "object identity"? Which aspect are you interested in when you compare the references stored in the lists?
If the same object has been stored in two separate ArrayList
containers and you want to check whether they are identical, use the ==
operator to check for object identity.
This will work since only the references are stored in the ArrayList
.
if I get instance1 from array1 and instance2 from array2, would they return different pointer locations or the same?
Yes, if they are referring to the same object, ==
will return true.
An ArrayList will store precisely what you give to it. If you give it a reference ("address") it will store that address. When you fetch the that particular element, you'll get the same reference back.
Sample snippet:
Object o = new Object();
List<Object> l1 = new ArrayList<Object>();
List<Object> l2 = new ArrayList<Object>();
l1.add(o);
l2.add(o);
Object o1 = l1.get(0);
Object o2 = l2.get(0);
System.out.println(o1 == o2); // prints true
In a sense the ArrayList does however make a copy (of the reference!) As this snippet illustrates, the list is not affected when changing s
:
String s = "hello";
List<String> l = new ArrayList<String>();
l.add(s);
s = "world";
System.out.println(l.get(0)); // prints hello
Basically ==
compares content of variables. Since the content of a reference is an "address", comparing references using ==
will yield true if and only if the two references refer to the same object.
o1.equals(o2)
on the other hand compares the actual objects which o1
and o2
refer to.
Does it store a pointer location as the key or the actual object? If I have 2 objects with identical content but are actually different objects, would it still work?
It uses the actual object as key. If you have two separate objects which are equal
, then you can use either one to retrieve the value stored for that key.
I'm pretty sure the == operator looks at the pointer location
This is nitpicking, but, no, ==
looks at pointer content, or object locations.
精彩评论