• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check the type of a variable

 
Alex Alcalde
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alex Alcalde
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot indeed !!! owe you a drink!!!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ryan McGuire
Ranch Hand
Posts: 1068
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic