The Artima Developer Community
Sponsored Link

NullPointerException vs. MyObject != null

Advertisement

Advertisement

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:

NullPointerException vs. MyObject != null

Posted by Bryan E. Boone on 08 Jul 1998, 1:38 PM

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();
}
}
}





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us