Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to determine class of Object types

Reply
Thread Tools

How to determine class of Object types

 
 
electric sheep
Guest
Posts: n/a
 
      05-06-2004
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
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      05-06-2004
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
 
Reply With Quote
 
 
 
 
Tiggy
Guest
Posts: n/a
 
      05-06-2004
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



 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      05-06-2004
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

 
Reply With Quote
 
blmblm@myrealbox.com
Guest
Posts: n/a
 
      05-08-2004
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.
 
Reply With Quote
 
iamfractal@hotmail.com
Guest
Posts: n/a
 
      05-08-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to determine if an object exists in the CALL object George Javascript 7 10-03-2009 04:02 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
How to determine an object's class and properties? laredotornado@zipmail.com Javascript 1 05-20-2005 02:22 PM
nuby: determine method passed and determine the receiver that received the method Peņa, Botp Ruby 1 01-24-2004 07:51 PM



Advertisments