• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to find type of a primitive variable?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification, Jim.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey jim
Can u explin in detail what u intend to convey.. Sorry for been inquisitive..
 
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic