How to determine class of Object types

E

electric sheep

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
 
C

Christophe Vanfleteren

electric said:
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.
 
T

Tiggy

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
 
T

Thomas Fritsch

electric said:
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
}
 
B

blmblm

Tiggy said:
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 ]
 
I

iamfractal

Thomas Fritsch said:
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
 

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
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top