How to determine class of Object types

Discussion in 'Java' started by electric sheep, May 6, 2004.

  1. Hi, i'm just curious, is there some way to determine the "most specific"
    type of an Object type ?

    For example, the URL's getContent() method returns an Object.
    Is there a way to "query" that object, and find out if it is a String,
    or an Image, or whatever ?
    I'd like to do something like:
    String foo = (String) my_url.getContent();
    But I want to make sure I have a String, and not an Image.

    cheers,
    e
     
    electric sheep, May 6, 2004
    #1
    1. Advertising

  2. electric sheep wrote:

    > Hi, i'm just curious, is there some way to determine the "most specific"
    > type of an Object type ?
    >
    > For example, the URL's getContent() method returns an Object.
    > Is there a way to "query" that object, and find out if it is a String,
    > or an Image, or whatever ?
    > I'd like to do something like:
    > String foo = (String) my_url.getContent();
    > But I want to make sure I have a String, and not an Image.
    >
    > cheers,
    > e


    Object o = myUrl.getContent();
    if(o instanceof String) {
    String s = (String)foo;
    }

    instanceof will be true for instances of String, and its subclasses (of
    which there are none offcourse). You can also use it with an interface, eg:

    if(o instanceof List) {
    ....
    }

    Will be true for all instances that implement List.

    There's also the getClass() method of Object, if you need to know the
    specific class of the instance.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, May 6, 2004
    #2
    1. Advertising

  3. electric sheep

    Tiggy Guest

    You can know the type of the reference of the object by using the
    combination of methods getClass().getName() on an object. Below what you
    could get if you have 2 classes A and B with B extending A:

    public class TestObjectType {

    public static void main(String s[]) {
    B b = new B();
    A a1 = new B();
    A a2 = new A();
    System.out.println(b.getClass().getName());
    System.out.println(a1.getClass().getName());
    System.out.println(a2.getClass().getName());
    }
    }
    class A {
    }
    class B extends A {
    }

    This will return

    B

    B

    A

    You can either use the instanceof operator but take care to handle the
    exception. It throws an exception if the left argument cannot be cast in the
    type of the right argument.

    Tiggy

    "electric sheep" <> wrote in message
    news:hZrmc.14886145$...
    > Hi, i'm just curious, is there some way to determine the "most specific"
    > type of an Object type ?
    >
    > For example, the URL's getContent() method returns an Object.
    > Is there a way to "query" that object, and find out if it is a String,
    > or an Image, or whatever ?
    > I'd like to do something like:
    > String foo = (String) my_url.getContent();
    > But I want to make sure I have a String, and not an Image.
    >
    > cheers,
    > e
     
    Tiggy, May 6, 2004
    #3
  4. electric sheep wrote:

    >Hi, i'm just curious, is there some way to determine the "most specific"
    >type of an Object type ?
    >
    >For example, the URL's getContent() method returns an Object.
    >Is there a way to "query" that object, and find out if it is a String,
    >or an Image, or whatever ?
    >I'd like to do something like:
    >String foo = (String) my_url.getContent();
    >But I want to make sure I have a String, and not an Image.
    >
    >cheers,
    >e
    >

    Object foo = my_url.getContent();
    if (foo instanceof String)
    {
    String s = (String) foo;
    // do something with s
    }

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, May 6, 2004
    #4
  5. electric sheep

    Guest

    In article <409a51ea$0$21670$>,
    Tiggy <> wrote:
    [ snip ]
    >
    >You can either use the instanceof operator but take care to handle the
    >exception. It throws an exception if the left argument cannot be cast
    >in the type of the right argument.


    Um. When I read this I thought "huh? no, the point of *having* an
    'instanceof' operator is so you can check this sort of thing without
    risking throwing an exception." That is, my understanding is that
    "x instanceof SomeClass" evaluates to "true" or "false" as appropriate
    but doesn't throw exceptions. But before posting I thought I'd better
    try an example ...

    In producing the allegedly simple example (below), I learned that
    the compiler may object to uses of the "instanceof" operator that
    can't possibly return "true" (e.g., some of the commented-out lines
    below caused compiler errors). But that's not the same thing as
    throwing an exception. This code runs without throwing exceptions
    (or it did for me anyway!).


    public class TestIt {
    public static void main(String[] args) {
    String s = "hello";
    Integer i = new Integer(10);
    foo(s);
    foo(i);
    /* won't compile! (second and third lines)
    System.out.println("s is a String? " + (s instanceof String));
    System.out.println("s is an Integer? " + (s instanceof Integer));
    System.out.println("i is a String? " + (i instanceof String));
    System.out.println("i is an Integer? " + (i instanceof Integer));
    */
    }
    private static void foo(Object o) {
    System.out.println(o.toString() +
    " a String? " + (o instanceof String));
    System.out.println(o.toString() +
    " an Integer? " + (o instanceof Integer));
    }
    }

    [ snip ]

    --
    | B. L. Massingill
    | ObDisclaimer: I don't speak for my employers; they return the favor.
     
    , May 8, 2004
    #5
  6. electric sheep

    Guest

    Thomas Fritsch <> wrote in message news:<>...
    > electric sheep wrote:
    >
    > >Hi, i'm just curious, is there some way to determine the "most specific"
    > >type of an Object type ?
    > >
    > >For example, the URL's getContent() method returns an Object.
    > >Is there a way to "query" that object, and find out if it is a String,
    > >or an Image, or whatever ?
    > >I'd like to do something like:
    > >String foo = (String) my_url.getContent();
    > >But I want to make sure I have a String, and not an Image.
    > >
    > >cheers,
    > >e
    > >

    > Object foo = my_url.getContent();
    > if (foo instanceof String)
    > {
    > String s = (String) foo;
    > // do something with s
    > }



    (Yeuch!)

    Casting.

    instanceof.

    Despisable.

    Understandable as the initial question and subsequent answers are,
    doesn't this all just show up how nasty that URL.getContent() is?

    They should have had a URLContent class with specific methods, and let
    Miss Poly Morphism do her stuff.

    ..ed

    www.EdmundKirwan.com
     
    , May 9, 2004
    #6
    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. E11
    Replies:
    1
    Views:
    4,788
    Thomas Weidenfeller
    Oct 12, 2005
  2. Martin
    Replies:
    6
    Views:
    595
    yurec
    Apr 2, 2008
  3. Peña, Botp
    Replies:
    1
    Views:
    242
    Robert Klemme
    Jan 24, 2004
  4. Replies:
    1
    Views:
    131
  5. George
    Replies:
    7
    Views:
    160
    Thomas 'PointedEars' Lahn
    Oct 3, 2009
Loading...

Share This Page