About Question enthuware.ocajp.i.v7.2.1060 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1060 :
Its strange how final modifier is permitted here since the variable o2 will change its values during the loop.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.Michailangelo wrote:Its strange how final modifier is permitted here since the variable o2 will change its values during the loop.
But yes, it does look strange
![Smile :)](./images/smilies/icon_e_smile.gif)
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1060 :
Will questions on Collections and iterators be included in the Associate Programmer test (1Z0-803)?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.ETS user wrote:Will questions on Collections and iterators be included in the Associate Programmer test (1Z0-803)?
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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?
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?
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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
"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
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
Iterator it = //get iterator somehow
while(it.hasNext()){
it.next();
}
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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?).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.
Please let me know whether my understanding is correct or not. Thank you!
Sincerely,
James
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
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.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
Yes, each one goes out of scope after that iteration is over.
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Sun Feb 18, 2024 7:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
I thought it is like a cursor over a data set.
-
- Site Admin
- Posts: 10102
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 30
- Joined: Sun Feb 18, 2024 7:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1060 :
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.
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.
Who is online
Users browsing this forum: Bing [Bot] and 21 guests