Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Mon Oct 01, 2012 1:57 pm
by Michailangelo
Its strange how final modifier is permitted here since the variable o2 will change its values during the loop.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Mon Oct 01, 2012 2:09 pm
by admin
Michailangelo wrote:Its strange how final modifier is permitted here since the variable o2 will change its values during the loop.
In this case, the scope of o2 ends at the end of the loop block. So it is like declaring a new variable (with the same name, of course) at the start of each iteration. And that is why it is ok to make it final.

But yes, it does look strange :)

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Tue Jan 08, 2013 1:28 pm
by ETS user
Will questions on Collections and iterators be included in the Associate Programmer test (1Z0-803)?

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Tue Jan 08, 2013 6:37 pm
by admin
ETS user wrote:Will questions on Collections and iterators be included in the Associate Programmer test (1Z0-803)?
No, we don't expect any tough questions on Collections and Iterators. But it would be good to know the basic concepts that touch the topics covered on the exam such as this one.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Wed Feb 20, 2013 7:25 pm
by Guest
Object o = null;
Collection c = //valid collection object.
int[][] ia = //valid array

option 4 :
for(int i : ia[0]){ }
Since ia[0] is an array of ints, this is valid. (It may throw a NullPointerException or ArrayIndexOutOfBoundsException at runtime if ia is not appropriately initialized.)

ia is a two dimensional array right? How can int i be assigned ia[0]?
ia[0] is pointing to an array of ints right?

What am I missing?

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Wed Feb 20, 2013 7:43 pm
by admin
Yes, ia is a two dimensional array. So ia[0] is a 1 dimensional array. Therefore, when you iterate through each element of ia[0], you get ints. It is those ints, which you assign to i one by one in the for loop and not ia[0] itself.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Wed Feb 20, 2013 8:46 pm
by Guest
Of Course...:-)
Thanks.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Mon Nov 25, 2013 10:49 am
by javaman
I hope this is not part of 1Z0-803 since I don't understand how (or even if) an interator object can be used within an enhanced for-loop after this explanation:

"c.iterator() does not return any Collection. Note that the following would have been valid: Collection<Iterator> c = //some collection that contains Iterator objects for(Iterator it : c){ }"

If I do this
ArrayList<Integer> arr = new ArrayList<>();
arr.add(0);
arr.add(1);
arr.add(2);
Iterator<Integer> it = arr.iterator();
for(Iterator i : it){
System.out.println("it.next()= " + it.next());
}

I get

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - for-each not applicable to expression type
required: array or java.lang.Iterable

How should I define an interator so I can use it in an enhanced for loop??

Thanks,
Marc

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Mon Nov 25, 2013 1:42 pm
by admin
You can't use an iterator in a for loop. If you have an iterator, you can do:

Iterator it = //get iterator somehow
while(it.hasNext()){
it.next();
}


HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Tue Feb 07, 2017 2:53 pm
by jamesmccreary
In this case, the scope of o2 ends at the end of the loop block. So it is like declaring a new variable (with the same name, of course) at the start of each iteration. And that is why it is ok to make it final.
Paul, you specify that the scope of o2 ends at the end of the loop block (I'm assuming you mean after all the iterations have completed and not after each iteration). Thus, how can you have redundant final Object o2 references? Are both final variables and final references not reassignable? Final references may be different than final variables (in which case I believe the reference simply points to a new object and the original object is eligible for garbage collection?).

Please let me know whether my understanding is correct or not. Thank you!

Sincerely,
James

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Tue Feb 07, 2017 9:37 pm
by admin
I am not sure I understand your question correctly but it seems you are confusing multiple things:
1. GC has nothing to do with whether the reference is final or not. An object will be GCed if there is no reference to that object.
2. In Java, a non-primitive variable is also known as a reference. There is no distinction between a variable and a reference. It is a "reference variable". You cannot get a reference to any object without having a variable. A primitive variable is just a variable. There is no reference for primitives.
3. Now, when you declare a variable final that means you can't change its value. If the variable is a non-primitive, its value is nothing but a reference, and if it is a primitive variable, its value is just the value. There is nothing like a "final reference". The keyword final applies to variables only (it could be a reference variable or a primitive variable).

In the given code, o2 is final for each iteration. Inside the for loop, you cannot make o2 point to another object. For example:
for(final Object o2 :c){
o2 = ""; //This is invalid because o2 is final.
}

for(Object o2 :c){
o2 = ""; //This is ok because o2 is not final.
}

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Wed Feb 08, 2017 11:23 am
by jamesmccreary
Yes, I believe I understand your explanation. In essence, your for loop is iterating over each object in the Collection c, and simply creating a final reference variable o2 for each one? Perhaps this is where my question lay, are we creating multiple final o2 references; if so, that is allowed?

Lastly, as long as we do not assign these references to anything, then the code compiles successfully?

Apologies if it is is obvious and not clicking yet, thank you for your patience.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Wed Feb 08, 2017 8:31 pm
by admin
Yes, each one goes out of scope after that iteration is over.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Sun Apr 21, 2024 9:10 pm
by likejudo
Is a new final variable created each time? I thought the same variable is reused in the for-each loop iterations.
I thought it is like a cursor over a data set.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Sun Apr 21, 2024 9:12 pm
by admin
So if you declare a variable in a method and if that method is called a 100 times, how many times is that variable created? The same thing happens when you declare a variable inside a loop.

Re: About Question enthuware.ocajp.i.v7.2.1060 :

Posted: Mon Apr 22, 2024 5:43 am
by likejudo
I thought the initial assignment is always before the loop?
As I have understood, the for loop is equivalent to a while loop with the assignment happening once just above the conditional. That would make a different from a method body

Assignment;
While(conditional) {
}

Correction. After thinking a bit I realized this is a for-each loop. I wonder how it is equivalent to the while loop. I don't recall reading the equivalence (as I described above) with the while loop.