• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to check the type of a variable

 
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE 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!!!
 
(instanceof Sidekick)
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.
 
Bartender
Posts: 1197
22
  • 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 ]
 
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?
 
Ranch Hand
Posts: 230
  • 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.
    Bookmark Topic Watch Topic
  • New Topic