*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to find type of a primitive variable? 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 "how to find type of a primitive variable?" Watch "how to find type of a primitive variable?" New topic
Author

how to find type of a primitive variable?

Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
If the variable refers to object, then we can find the class of that object using method obj.getClass()

If the variable is a primitive, how to find its type? whether it is a byte or int or short? Any such method available for primitives? like typeOf(obj)

anybody know?
Lakshmanan Arunachalam
Ranch Hand

Joined: Nov 02, 2005
Posts: 99
I dont think in realworld there is a need for it.

For only objects, we dont know what exact the class that belongs to dueto runtime bindings. For primitive variables, they are compile time types.

ie a variable of int type can't be of type long.


Regards<br />Lakshmanan<br />IBM-OOAD & UML, SCEA-I
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

A typeOf() doesn't really make sense for primitive types. typeOf(int) would just return int and typeOf(long) would return long so what's the point ?

This may interest you: Class Literal. This is used primarily when invoking methods using reflection but as far as I know, it is not part of the requirements for SCJP.



Junilu - [How to Ask Questions] [How to Answer Questions]
Don Morgan
Ranch Hand

Joined: Jul 24, 2003
Posts: 84
I am not sure why you would want to do this. In any situation (at least that I can think of!), you would know the type of variable by looking at the source code.

Anyway, one possible use would be for debugging/troubleshooting, since it would be easier to dump out the type of variable to a console or logfile, as opposed to having to always cross reference back to the source.

So, with that in mind, here is simple solution which really just lets the compiler/JVM sort it all out. The class which actually determines the type of primitive is simply called "PrimitiveTester". All is does is have a bunch of static methods overloaded and returning a simple integer which represents the type of argument it was called with. As you can see, it would be easy to extend this type of solution, say with an overloaded getMax function which would return the max value for whatever type of variable it was called with (such as byte, short, int, long ).



Here is a short sample program which drives it:

And here is the sample output:


Anyway, hope this helps.

[ December 15, 2005: Message edited by: Don Morgan ]
[ December 15, 2005: Message edited by: Don Morgan ]

Don Morgan, Founder
www.DeveloperAdvantage.com - FREE Audiobooks for Software Developers
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sorry to hijack this thread, but since the original poster's question was about something no one would ever use, and not on the exam anyway, I thought I'd respond to a comment that is relevant to the exam:

[Junilu]: This may interest you: Class Literal. This is used primarily when invoking methods using reflection but as far as I know, it is not part of the requirements for SCJP.

As a matter of fact, class literals can appear on the 5.0 exam. They are of interest in relation to synchronization - it's useful to understand that a static synchronized method is equivalent to a static method with a synchronized block, if that block acquires a lock on the Class instance for whatever class you're in. Which makes it useful to be able to refer to that instance with a class literal. I.e.

is equivalent to

[ December 15, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

Thanks for the clarification, Jim.
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
Hey jim
Can u explin in detail what u intend to convey.. Sorry for been inquisitive..
 
GeeCON Prague 2014
 
subject: how to find type of a primitive variable?