S
S
sdevalex2011-10-17 00:12:42
Java
sdevalex, 2011-10-17 00:12:42

Java - "Negative" return

There is a method that looks for an object ... but how do you know that the object is not found? I thought of only using an exception, but they, by analogy with other languages, slow down the program ... Is there a secret trick?

The code is like this...

public SomeObject findObject()
{
     //...

     if(findResult)
          return object;

     //...???
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman, 2011-10-17
@WNeZRoS

return null;
won't fit?

T
taviscaron, 2011-10-17
@taviscaron

“Program brakes” when using exceptions are critical, except in 0.0..001% of cases.
and of course return null; As suggested above, it works well.

Z
Zorkus, 2011-10-26
@Zorkus

I do, use either return null or define a custom NullObject type, and return that in that case.
en.wikipedia.org/wiki/Null_Object_pattern

T
theme, 2012-09-27
@theme

public static final SomeObject NOT_FOUND = new SomeObject (..) ... return NOT_FOUND
inside the system (if the method is not called by an external client), an empty object is green, which does nothing to prevent null checks.
Just returning null is practically dooming yourself to NPE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question