Detecting Type of Object

H

Hal Vaughan

I'm sure there's a term for how to do this, but I don't know what to search
under.

If I have an Object that could be a string or a Swing component or something
else, is there a way to find out what type of object it is? I know there
is Object.toString(), but if the object is a string, it just returns the
string. I'd like to be able to take an unknown object and determine what
it is.

Thanks!

Hal
 
A

Andrew Thompson

Hal said:
I'm sure there's a term for how to do this, but I don't know what to search
under.
*

If I have an Object that could be a string or a Swing component or something
else, is there a way to find out what type of object it is?

* 'instanceof'.

Andrew T.
 
H

Hal Vaughan

Andrew said:
* 'instanceof'.

Andrew T.

I don't get it. I can't find any case of an "instanceOf()" method in the
API docs.

In the meanwhile, I just finished testing something else and found I can
take an object and do Object.getClass(), then do Class.getName(). It was
just roundabout and I was looking only directly under Object.

Hal
 
A

AndrewMcDonagh

Hal said:
I don't get it. I can't find any case of an "instanceOf()" method in the
API docs.

In the meanwhile, I just finished testing something else and found I can
take an object and do Object.getClass(), then do Class.getName(). It was
just roundabout and I was looking only directly under Object.

Hal


if (obj instanceof Dog) {
Dog dog = (Dog) obj;
dog.bark();
}

the 'instaanceof' operator does the same as the 'instanceof()' method
found on the Class Object.

i.e.

if( obj.getClass().instanceof(Dog.getClass() ) {
....
}
 
R

rutski89

Hal said:
I'm sure there's a term for how to do this, but I don't know what to search
under.

If I have an Object that could be a string or a Swing component or something
else, is there a way to find out what type of object it is? I know there
is Object.toString(), but if the object is a string, it just returns the
string. I'd like to be able to take an unknown object and determine what
it is.

Thanks!

Hal

There are two ways to approach this:

1) Use Java's builtin "instanceof" keyword, documented here:
http://mindprod.com/jgloss/instanceof.html

2) Use the static java.lang.Class.isInstance(Object obj) method,
documented here:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#isInstance(java.lang.Object)
 
A

Andrew Thompson

AndrewMcDonagh said:
...
the 'instaanceof' operator does the same as the 'instanceof()' method
found on the Class Object.

Huh! I never realised there was also an instanceOf()
method as well, but then..

I figured if Hal's Google was as good as the one
'installed on my PC', it might have led him to..
<http://www.google.com.au/search?hl=en&q=instanceof&meta=>
1st hit is Roedy's page on instanceof, and the second is one of
the many rants on why we should not use it.. ;-)

Andrew T.
 
A

AndrewMcDonagh

Andrew said:
Huh! I never realised there was also an instanceOf()
method as well, but then..

Yeah it surprised me. I only found it when I was looking for its
partners (in crime ;-) ) 'isAssignableFrom(Class cls)'
I figured if Hal's Google was as good as the one
'installed on my PC', it might have led him to..
<http://www.google.com.au/search?hl=en&q=instanceof&meta=>
1st hit is Roedy's page on instanceof, and the second is one of
the many rants on why we should not use it.. ;-)

To be fair, Hal said he wasn't sure what the term Java used, so it was
more harder for him to google.
 
H

Hal Vaughan

AndrewMcDonagh said:
Yeah it surprised me. I only found it when I was looking for its
partners (in crime ;-) ) 'isAssignableFrom(Class cls)'

To be fair, Hal said he wasn't sure what the term Java used, so it was
more harder for him to google.

Bingo! There are a lot of disadvantages to being self taught in some
program languages. I wasn't sure what to search for at first. Then, after
I posted, I found the solution I posted. When I read Andrew Thompson's
first post, I checked the Java API docs (for Java 2, still using it for
compatibility) and found no "instanceof" or "instanceOf" in there at all.
If it wasn't included in the API, I saw no need to Google. I didn't
realize it was an operator -- I had never seen a word about it in anything
I had read. I also had a working solution. While I love to be a purist
when I can, I'm running a small business based on the software I write and
it boils down to a working solution now beats another solution in the
future. If it works now, I can put the program into work and maybe spend
less than 14 hours at the computer that day.

Hal
 
A

Andrew Thompson

Hal said:
..... When I read Andrew Thompson's
first post, I checked the Java API docs (for Java 2, still using it for
compatibility) and found no "instanceof" or "instanceOf" in there at all.
If it wasn't included in the API, I saw no need to Google.

I find that statement very odd, given your very first post
stated "but I don't know what to search under. ..."
To which my answer was "instanceof".

Andrew T.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Hal said:
I'm sure there's a term for how to do this, but I don't know what to search
under.

If I have an Object that could be a string or a Swing component or something
else, is there a way to find out what type of object it is? I know there
is Object.toString(), but if the object is a string, it just returns the
string. I'd like to be able to take an unknown object and determine what
it is.

You have already gotten a lot of instanceof operator
references.

Besides that:

o.getClass().getName()

could be relevant.

Arne
 
H

Hal Vaughan

Andrew said:
I find that statement very odd, given your very first post
stated "but I don't know what to search under. ..."
To which my answer was "instanceof".

I'm not clear if the intent here, and with the original Google comment, is
to ridicule me, to understand what is going on, or to disseminate
information.

In the section you quoted, I pointed out that I checked the API docs and
found nothing on "instanceof". I also pointed out I am self taught. I had
a really, really bad course in Fortran in about 1982, and one course in Vax
11/780 assembler. Then I did a little 6502 Assembler programming, then
didn't touch a line of code for way over a decade, and now am teaching
myself. That leaves a lot of holes in understanding many concepts. For
me, OOP and Java has been the hardest to learn. In short, there are a lot
of holes and I'm not always aware there are holes in what I know. When you
cited "instanceof", I had no idea it was an operator because I had never
seen anything about it. In that context, from my scattered background, the
only place that seemed logical to find it was in the API. That's where I
looked, that's where I didn't find it. I've gotten misdirected suggestions
before, such as people telling me to try methods that weren't there, and
that, at the time was my best guess as to what was going on. I was wrong,
but that happens to all of us from time to time.

Hal
 
Z

Zaph0d

You have already gotten a lot of instanceof operator
references. ....
o.getClass().getName()
....

As long as you're searching - yahooing, msning or seraching via google
(google doesn't like you to verbify it's name) you might want to look
up "Reflection and preformance".
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Zaph0d said:
...

As long as you're searching - yahooing, msning or seraching via google
(google doesn't like you to verbify it's name) you might want to look
up "Reflection and preformance".

Maybe you should look at a calendar. It is 2006 not 1996.

Reflection is widely used in a lot of the most popular
Java frameworks today.

That does not make it a good fit in all cases, but practically
nothing is.

Arne
 
D

Dale King

AndrewMcDonagh said:
the 'instaanceof' operator does the same as the 'instanceof()' method
found on the Class Object.

if( obj.getClass().instanceof(Dog.getClass() ) {

For the sake of correctness, the method in Class is called isInstance.
 

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

Latest Threads

Top