I have a question about this foreach:
for(Element e : nullProof(setOfElements)){
// do stuff
}
Let's assume I defined the function, nullProof, to return a non-null Set (because I heard you shouldn't pass null into an enhanced for-loop. Is this true?):
private Set<Element> nullProof(Set<Element> s){
return s == null ? Collections.<Element> emptySet() : s;
}
My question is...is it safe to call the nullProof function within the foreach? Specifically, is the following header equivalent to:
for(Element e : setOfElements){ //assuming setOfElements != null
I was wondering if someone could point me to some Java standard that says this is defined behavior.
Furthermore, can someone explain what actually happens "behind the scenes" when this foreach is called?
Let's say setOfElements has size 6. For each iteration through setOfElements, does the JVM look up setOfElements 6 different times, or does it create a copy of that set and refer to the copy? I'm trying to understand the behavior. Thanks for the help!