shawn-mcgee
shawn-mcgee

Reputation: 77

Using equals() and == in an enhanced for loop

Lets say there is an array that contains a series of objects,

 Object[] list;

and a method designed to iterate through this array, for example

public boolean contains(Object e)
{
    for(Object e_:list)
        if(e_.equals(e))
            return true;
    return false;
}

What I'm confused about is how the for loop iterates the array. When iterating, does it assign e_ to the same memory location as list[index] or is e_ a new object cloned from list[index], because what I want to do is use == instead of equals() so that I can specifically call the object and not risk it being equal() to another. I understand I could override equals() and make it final to prevent inheritance from being an issue, but I would like to understand how iteration works inside of an enhanced for loop.

Upvotes: 1

Views: 2290

Answers (4)

David Conrad
David Conrad

Reputation: 16379

The enhanced for loop can iterate over collections (or anything that implements Iterable) or arrays. In the case of Iterable, it does it by getting the Iterator from the object and repeatedly calling its hasNext() and next() methods, but the case you're interested in is arrays.

In the case of an array, it gets the length of the array, initializes an index to 0, and then repeatedly increments the index, each time looking up an item from the array, until the index is greater than or equal to the length of the array.

In other words, it is equivalent to the loop:

for (int i = 0; i < array.length; i++) {
    Object e_ = array[i];
}

So, the objects that are assigned to e_ are the actual objects from the array (references, that is), and not clones or otherwise newly created objects. By the way, this is also true of iterating over a collection, since the next() method of the iterator will return references from the collection, not clones or copies.

Thus, if e is an actual object that was put into the array, and not merely one that can be expected to compare equals() to one in the array, it is safe to compare them as e_ == e.

(Note: You could find out about the implementation either by reading the Java Language Specification, or, if you don't have that kind of patience, by compiling a simple program and then looking at the disassembly of it with javap -v ClassName, although that would require learning Java bytecode.)

Upvotes: 0

lxcky
lxcky

Reputation: 1668

What I'm confused about is how the for loop iterates the array. When iterating, does it assign e_ to the same memory location as list[index] or is e_ a new object cloned from list[index]

Object[] someList = {...};
for(Object e:someList) {
}

Simply means, for each Object in someList assign it to variable e.

...because what I want to do is use == instead of equals() so that I can specifically call the object and not risk it being equal() to another.

When comparing objects, you must provide your own implementation of the object's equals() method. Using == for comparing equality of objects might return unexpected result. Please note that == just compares the reference address of the objects to determine if they're equal. It's usually used only for comparing primitive types in Java.

Read more about the equals() method and == operator here.

Upvotes: 2

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

== will check for equality of the reference, i.e. whether pointing to same object. Whereas, equals will check for equality pf object content, provided that's overridden in the class that you use.

Upvotes: 0

Maxaon3000
Maxaon3000

Reputation: 1129

There is rarely any cloning in Java. The assignment (=) operator in Java does not clone objects. A=B sets the reference A to the same value as B. So (A==B) is true. When you put objects into a list/array the objects are not cloned. When you iterate through an array it's the same reference values that you put in there.

Upvotes: 7

Related Questions