-
Enhancement
-
Resolution: Not an Issue
-
P4
-
None
-
5.0
-
x86
-
linux
A DESCRIPTION OF THE REQUEST :
The new for loop construct in Java 5 is great, but many people have quickly realized that one helpful enhancement would be to have the for loop check for a null collection or array and quietly skip the loop if it finds one. For example, instead of this:
List<Strings> list;
// ...
if (list!=null) {
for (String s: list) {
System.out.println(s);
}
}
Do this:
for (String s: list) { // if list==null, then skip over the list as though it was empty
System.out.println(s);
}
JUSTIFICATION :
The syntax is already in place to handle this. It would just avoid having to wrap so many for loops with a check for null first, which would make code more readable. The semantics also already make sense, i.e., if we read these for loops as "for each thing in this list, do that", then it makes sense that if the list is either empty OR NULL, then there is nothing in it, so nothing should be done. The CPU cost of one extra comparison at the beginning of the for loop to check for null is probably neglegible.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following code should quietly print nothing instead of throwing a NullPointerException:
String[] names = null;
for (String name: names) {
System.out.println(name);
}
ACTUAL -
Currently a NullPointerException is thrown when trying to iterate over a null array or collection.
---------- BEGIN SOURCE ----------
List<String> list = new ArrayList<String>();
String[] names = null;
for (String name: names) {
list.add(name);
}
assertEquals(0, list.size());
names = new String[] {"a", "b", "c"};
for (String name: names) {
list.add(name);
}
assertEquals(3, list.size());
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Check for null first, i.e.,
if (names!=null) {
for (String name: names) {
list.add(name);
}
}
The new for loop construct in Java 5 is great, but many people have quickly realized that one helpful enhancement would be to have the for loop check for a null collection or array and quietly skip the loop if it finds one. For example, instead of this:
List<Strings> list;
// ...
if (list!=null) {
for (String s: list) {
System.out.println(s);
}
}
Do this:
for (String s: list) { // if list==null, then skip over the list as though it was empty
System.out.println(s);
}
JUSTIFICATION :
The syntax is already in place to handle this. It would just avoid having to wrap so many for loops with a check for null first, which would make code more readable. The semantics also already make sense, i.e., if we read these for loops as "for each thing in this list, do that", then it makes sense that if the list is either empty OR NULL, then there is nothing in it, so nothing should be done. The CPU cost of one extra comparison at the beginning of the for loop to check for null is probably neglegible.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The following code should quietly print nothing instead of throwing a NullPointerException:
String[] names = null;
for (String name: names) {
System.out.println(name);
}
ACTUAL -
Currently a NullPointerException is thrown when trying to iterate over a null array or collection.
---------- BEGIN SOURCE ----------
List<String> list = new ArrayList<String>();
String[] names = null;
for (String name: names) {
list.add(name);
}
assertEquals(0, list.size());
names = new String[] {"a", "b", "c"};
for (String name: names) {
list.add(name);
}
assertEquals(3, list.size());
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Check for null first, i.e.,
if (names!=null) {
for (String name: names) {
list.add(name);
}
}
- duplicates
-
JDK-8243651 java for-each loop should handle nulls cleanly.
-
- Closed
-
- relates to
-
JDK-8243651 java for-each loop should handle nulls cleanly.
-
- Closed
-
-
JDK-6312085 enhanced-for statement (for-each loop) should support Iterator
-
- Closed
-