• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instanceof or a method for primitives

 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any method that behaves like the instanceof operator for primitive types?
For what purpose, does the wrapper classes have the TYPE (Class) constant?
 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrapper classes are useful whenever it is convenient to treat
a primitive data as if it were an object. because some methods
take only object parameters, in those cases to pass the primitive
data types, we need to convert them into wrapper classes and
pass them to those methods...,
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need for something parallel to instanceof for primitives because the compiler can always tell from the code what the type is. Its only reference types that have to be checked at runtime.
Bill
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry I didn�t ask what you are answering to me, the question is about the instanceof operator, not about wrapper classes, let�s try to be more clear.
If a have an Integer object I can find if it is an Integer by using the instanceof operator.
eg. Integer i = new Integer(1);
if (i instanceof Integer) ...
The question is: I can�t use the instanceof operator for primitive types but, do I have a method for knowing the exact type of a primitive?
The wrapper classes have a constant var named TYPE that is of Class type (reflection). I remember I read that there was a way for checking something like this with primitives...
In the Class class you have a isPrimitive() method but I want something more precise.
Thanks
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill: I posted the other reply without reading your answer.
Thanks, you are right, but I remember I read something a long time ago about this, but don�t remember exactly what it was. It doesn�t matter, I�ll try to find it, if exists, I�ll bring back to the forum again.
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic