Tuesday, 15 May 2012

java - Enhanced for loop and null check of objects -



java - Enhanced for loop and null check of objects -

lets have next code:

for (anyobject anyobject : anyobjectlist) { system.out.println(anyobject.dosomething()); }

my question is: read possible add together null @ to the lowest degree kind of list types. if im right: shouldn't there null check before access method of iterated class? im asking because have never seen far enhanced loop null check example:

for (anyobject anyobject : anyobjectlist) { if (anyobject != null) { system.out.println(anyobject.dosomething()); } }

actually you'd want know if there random null in list when there shouldn't be. first snippet throw exception indicating problem, sec consume error silently.

you this:

for (anyobject anyobject : anyobjectlist) { if (anyobject != null) { system.out.println(anyobject.dosomething()); } else { /* went wrong -- study error, debugging info, etc. */ } }

java for-loop foreach

No comments:

Post a Comment