Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

newbie: IsNothing in Java

Thread view: 
buu - 03 Nov 2006 21:36 GMT
Is there an any kind of checking if some object exist?
something like IsNothing check or somethin'??
Arne Vajhøj - 03 Nov 2006 21:42 GMT
> Is there an any kind of checking if some object exist?
> something like IsNothing check or somethin'??

if(o != null)

Arne
buu - 03 Nov 2006 21:45 GMT
>> Is there an any kind of checking if some object exist?
>> something like IsNothing check or somethin'??
>
> if(o != null)
>
> Arne

what about java.lang.class.IsInstance?
Oliver Wong - 03 Nov 2006 22:15 GMT
>>> Is there an any kind of checking if some object exist?
>>> something like IsNothing check or somethin'??
[quoted text clipped - 4 lines]
>
> 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
Simon Brooke - 03 Nov 2006 22:54 GMT
>>> Is there an any kind of checking if some object exist?
>>> something like IsNothing check or somethin'??
[quoted text clipped - 4 lines]
>
> 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.

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       ;; Woz: 'All the best people in life seem to like LINUX.'
       ;; <URL:http://www.woz.org/woz/cresponses/response03.html>

Daniel Dyer - 03 Nov 2006 23:12 GMT
>         if ( o instanceof MyClass)
>         // stuff
[quoted text clipped - 8 lines]
> (actually
> a NullPointerException) if o was null.

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

Dan.

Signature

Daniel Dyer
http://www.uncommons.org

Stefan Ram - 03 Nov 2006 23:53 GMT
>Is there an any kind of checking if some object exist?

 An object always exists.

 If it would not exist, it would not be an object.
Daniel Pitts - 04 Nov 2006 00:19 GMT
> >Is there an any kind of checking if some object exist?
>
>   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.
Mark Space - 04 Nov 2006 00:56 GMT
> 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).


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.