aspose file tools*
The moose likes Beginning Java and the fly likes How to check the type of a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to check the type of a variable" Watch "How to check the type of a variable" New topic
Author

How to check the type of a variable

Alex Alcalde
Greenhorn

Joined: May 13, 2005
Posts: 2
Hello there!!!

I would like to know how to check the type of a data, so that I can choose what to do with it. I specially need to know what type something I receive is, be it a string or an integer...

Thanx in advance,

Alex
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can test the type of an object at runtime with the operator instanceof:

The Class class also gives you a method:


Or you can use class literals to achieve much the same.

The difference with using class literals is that the test is exact, whereas instanceof and isInstance() are both true if the object compared is of the type of the test or a sub-class of that type.
[ May 13, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Alex Alcalde
Greenhorn

Joined: May 13, 2005
Posts: 2
Thanx a lot indeed !!! owe you a drink!!!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Note that this is one problem solved by overloaded methods. That is, two methods with the same name but different argument types. Consider ...

Now if somebody calls mangle( "String" ) it goes into the first method, but if they call mangle( new Integer(1) ) it goes into the second. The methods document what they accept, there is no "if" test on our part and we can add new methods for new types without touching the existing methods. It's all good.

Look at something like PrintStream in the library and see how many print methods there are. That's how they avoid instanceof.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1012
    
    3
Originally posted by Stan James:
Note that this is one problem solved by overloaded methods.


Also, you may want to consider subclassing when you find yourself using instanceof a lot.

if you have...



...you might consider making VectorThingy and HashtableThingy subclasses. Each of those subclasses would implement xyz(), and then method(), which will still live in the Thingy base class, will just call xyz() and the right functionality will be used.



Of course "instanceof" isn't the only flag that indicates when this should be done. ANY time you see more than one or two if/else that all use the same if condition that doesnt' change through course of a method, you should consider this type of subclassing.

e.g.
if (databaseBrand.equals("Oracle")) ...
if (currentUser.isAdminUser()) ...
if (protocol == Protocols.TCP_IP) ...
if (vehicle.class == MOTORCYCLE || vehicle.class == SNOWMOBILE) ...

Ryan

[EDIT: Added the abstract xyz() to Thingy]
[ May 19, 2005: Message edited by: Ryan McGuire ]
Mukherji Sandeep
Greenhorn

Joined: Dec 09, 2008
Posts: 6


Please check it. i and j are not same in this case. But i and j both are still Integer types. so then where is the difference?


Mukherji Sandeep<br />Trying Hard to learn java
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Mukherji Sandeep wrote:
Please check it. i and j are not same in this case. But i and j both are still Integer types. so then where is the difference?


They are not the same object. A new one is created for j when you get the intValue(). It is my understanding this basically does new Integer(j.intValue()) behind the scenes. They are meaningfully equivalent and I'm sure if you try .equals() you will see this.

Why reply to such an old thread anywho? In the future i would just create a new one.
 
 
subject: How to check the type of a variable