newbie: IsNothing in Java

Discussion in 'Java' started by buu, Nov 3, 2006.

  1. buu

    buu Guest

    Is there an any kind of checking if some object exist?
    something like IsNothing check or somethin'??
    buu, Nov 3, 2006
    #1
    1. Advertising

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


    if(o != null)

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Nov 3, 2006
    #2
    1. Advertising

  3. buu

    buu Guest

    "Arne Vajhøj" <> wrote in message
    news:454ba9c2$0$49199$...
    > buu wrote:
    >> 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?
    buu, Nov 3, 2006
    #3
  4. buu

    Oliver Wong Guest

    "buu" <> wrote in message news:eigabq$4j4$...
    >
    > "Arne Vajhøj" <> wrote in message
    > news:454ba9c2$0$49199$...
    >> buu wrote:
    >>> 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?


    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
    Oliver Wong, Nov 3, 2006
    #4
  5. buu

    Simon Brooke Guest

    in message <eigabq$4j4$>, buu ('') wrote:

    >
    > "Arne Vajhøj" <> wrote in message
    > news:454ba9c2$0$49199$...
    >> buu wrote:
    >>> 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?


    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.

    --
    (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>
    Simon Brooke, Nov 3, 2006
    #5
  6. buu

    Daniel Dyer Guest

    On Fri, 03 Nov 2006 21:54:17 -0000, Simon Brooke <>
    wrote:
    > 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.

    --
    Daniel Dyer
    http://www.uncommons.org
    Daniel Dyer, Nov 3, 2006
    #6
  7. buu

    Stefan Ram Guest

    "buu" <> writes:
    >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.
    Stefan Ram, Nov 3, 2006
    #7
  8. buu

    Daniel Pitts Guest

    Stefan Ram wrote:
    > "buu" <> writes:
    > >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.
    Daniel Pitts, Nov 3, 2006
    #8
  9. buu

    Mark Space Guest

    buu wrote:
    > 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).
    Mark Space, Nov 3, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CSharp

    IsNothing

    CSharp, Jun 30, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,787
    Ante Perkovic
    Jun 30, 2003
  2. peelman
    Replies:
    2
    Views:
    422
    anonymous
    Jan 13, 2005
  3. xarora
    Replies:
    5
    Views:
    963
    Roedy Green
    Jul 28, 2005
  4. Replies:
    0
    Views:
    309
  5. pantagruel
    Replies:
    1
    Views:
    673
    pantagruel
    Mar 10, 2008
Loading...

Share This Page