1

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.

3
  • You are on programmers forum. What are "layman's terms" for you? Have you ever looked at Iterator class?
    – PM 77-1
    Oct 25, 2014 at 4:30
  • When I was on other places, it was explained in code, and I did get a bit bogged down in it... So, it would be best to explain using commentary and if using examples... please make them as simple as you can make them. Oct 25, 2014 at 4:32
  • You have not answered my 2nd question about Iterator class.
    – PM 77-1
    Oct 25, 2014 at 4:39

4 Answers 4

7

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.

1
  • +1. Explicitly, the Object[] list and Object e_ only store object references, not actual object instance data. References can be copied freely, and == checks if they refer to the same instance on the heap. Oct 25, 2014 at 4:53
2

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.

1
  • 1
    You absolutely can and, sometimes, should use == with objects as long as you understand that you are comparing object identities.
    – PM 77-1
    Oct 25, 2014 at 4:37
0

== 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.

0

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.