|
This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
a useful side-step
Posted by Raghu Havaldar on May 05, 1999 at 11:33 AM
> I enjoyed the article on Exceptions. I've been wrestling with > which is better for code that gets executed many times. > Say, I have a class MyClass which function (doit1() or doit2()) > is more efficient? > class MyClass { > MyObject myObj; > ... > ... > void doit1() { > if(myObj == null) { > myObj = new MyObject(); > } > myObj.process(); > } > void doit2() { > try { > myObj.process(); > } > catch(NullPointerException e) { > myObj = new MyObject(); > myObj.process(); > } > } > } Without addressing the 'optimization' issue, I would prefer to have a Assert class to check for object references been 'null'. Such as: Assert.isNull(objectRef). This method would throw an 'unchecked' exception if the reference was null. Raghu
Replies:
|