Java Forum / General / September 2006
Detecting Type of Object
Hal Vaughan - 10 Sep 2006 08:52 GMT 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
Andrew Thompson - 10 Sep 2006 09:26 GMT > 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.
Hal Vaughan - 10 Sep 2006 10:09 GMT >> I'm sure there's a term for how to do this, but I don't know what to >> search under. [quoted text clipped - 7 lines] > > 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
AndrewMcDonagh - 10 Sep 2006 10:44 GMT >>> I'm sure there's a term for how to do this, but I don't know what to >>> search under. [quoted text clipped - 14 lines] > > 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() ) { .... }
Andrew Thompson - 10 Sep 2006 11:48 GMT > >>> I'm sure there's a term for how to do this, but I don't know what to > >>> search under. [quoted text clipped - 3 lines] > >>> something else, is there a way to find out what type of object it is? > >> * 'instanceof'. ...
> > I don't get it. I can't find any case of an "instanceOf()" method in the > > API docs. ..
> 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.
AndrewMcDonagh - 10 Sep 2006 16:43 GMT >>>>> I'm sure there's a term for how to do this, but I don't know what to >>>>> search under. [quoted text clipped - 12 lines] > 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.
> Andrew T. Hal Vaughan - 10 Sep 2006 18:38 GMT >>>>>> I'm sure there's a term for how to do this, but I don't know what to >>>>>> search under. [quoted text clipped - 23 lines] > 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
Andrew Thompson - 10 Sep 2006 18:45 GMT >..... 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.
Hal Vaughan - 10 Sep 2006 21:51 GMT >>..... When I read Andrew Thompson's >> first post, I checked the Java API docs (for Java 2, still using it for [quoted text clipped - 4 lines] > 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
Dale King - 12 Sep 2006 13:41 GMT > 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.
 Signature Dale King
rutski89 - 10 Sep 2006 11:02 GMT > I'm sure there's a term for how to do this, but I don't know what to search > under. [quoted text clipped - 8 lines] > > 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.lan g.Object)
Arne Vajhøj - 10 Sep 2006 19:01 GMT > I'm sure there's a term for how to do this, but I don't know what to search > under. [quoted text clipped - 4 lines] > 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
Zaph0d - 11 Sep 2006 06:11 GMT > 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".
Arne Vajhøj - 11 Sep 2006 14:06 GMT >> You have already gotten a lot of instanceof operator >> references. [quoted text clipped - 5 lines] > (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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|