File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof or a method for primitives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "instanceof or a method for primitives" Watch "instanceof or a method for primitives" New topic
Author

instanceof or a method for primitives

Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
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

Joined: Jun 13, 2000
Posts: 176
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...,

Bhasker Reddy
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Jun 28, 2000
Posts: 477

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

Joined: Jun 28, 2000
Posts: 477
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instanceof or a method for primitives