newbie: IsNothing in Java

B

buu

Is there an any kind of checking if some object exist?
something like IsNothing check or somethin'??
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

buu said:
Is there an any kind of checking if some object exist?
something like IsNothing check or somethin'??

if(o != null)

Arne
 
O

Oliver Wong

buu said:
what about java.lang.class.IsInstance?

That determines, for a given class, whether a given object is an
instance of that class.

E.g.:

final Class stringClass = String.class;
assert stringClass.isInstance("Hello World") == true;
assert stringClass.isInstance(new Integer(42)) == false;

- Oliver
 
S

Simon Brooke

what about java.lang.class.IsInstance?

usually used in the shorthand syntax

if ( o instanceof MyClass)
// stuff

which returns true if o is an instance of MyClass or some subclass of
MyClass. This isn't what you want, because

(i) o may be an instance of some class which is unrelated to MyClass and
still not be null, and

(ii) o instanceof java.lang.Class would throw a runtime exception (actually
a NullPointerException) if o was null.

Of course instead of

if ( o != null)
{
// stuff
}

you could equally use

try
{
if ( o instanceof Class)
{
// stuff
}
}
catch ( NullPointerException npe)
{
// ignore
}

but I think you'll agree that would be woefully over complex, and would
make it problematic to debug actual null pointer exceptions which happened
in your code.
 
D

Daniel Dyer

if ( o instanceof MyClass)
// stuff

which returns true if o is an instance of MyClass or some subclass of
MyClass. This isn't what you want, because

(i) o may be an instance of some class which is unrelated to MyClass and
still not be null, and

(ii) o instanceof java.lang.Class would throw a runtime exception
(actually
a NullPointerException) if o was null.

This will actually just resolve to false. There won't be an exception.

Dan.
 
D

Daniel Pitts

Stefan said:
An object always exists.

If it would not exist, it would not be an object.

So true. However, I think the intent of the original question was "How
can I tell if my reference points to an object or not."

The correct answer to that question is:
When you have a reference, it either points to an object, or a
"magical" value called "null". So, if your reference points to null,
then it doesn't point to an object.

if (myObject != null) {
System.out.println("By the property of the converse error. myObject
exists, therefore it thinks.");
}

And for the record, "null instanceof Object" evaluates to false, but it
is a poor way to check for null.
 
M

Mark Space

buu said:
Is there an any kind of checking if some object exist?
something like IsNothing check or somethin'??

What language is IsNothing?

Java is not JavaScript; it doesn't allow objects (or variable names) to
be referenced if they're undefined. You'll have to resort to other
means if you want script-like behavior with undefined objects. ( ==
null and instanceOf, above, won't do undefined variables either).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top