wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes primitive type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "primitive type "introspection".." Watch "primitive type "introspection".." New topic
Author

primitive type "introspection"..

edi kapllani
Greenhorn

Joined: Sep 14, 2001
Posts: 5
I am trying mathematics operations and relations between primitives in Java. Can you help me to know anny way which shows me in anny moment the primitive type which I am elaborating.
thank you!
ciao!


edi
Jody Seigle
Greenhorn

Joined: Sep 29, 2000
Posts: 24
I've searched the API for something that will do this easily and can't find a straight-forward way to do it.
If you can get a wrapper class for the primitive then you can call getClass().toString() on it, to tell which type you have. It would be nice if the java.lang.Number class had a getInstance() method for each of the java primitives that would return an Instance of the correct wrapper.
such as Double getInstance(double d)
Float getInstance(float f)
so that at runtime the correct method would be called and you could get the correct instance. If you wanted to, you could write this static class yourself consisting of a static getInstance method for each of the 8 primitives.
Should only be ~20 lines of code anyway.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes, you can do this using reflection. We had a long talk about this once here: http://www.javaranch.com/ubb/Forum1/HTML/001421.html


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jody Seigle
Greenhorn

Joined: Sep 29, 2000
Posts: 24
Not to bring up an old topic... but that old discussion only gives you the corresponding Class reference for a primitive type. Not the type of the result of a conversion operation dynamically.
I'm assuming he's wanting this for testing conversion/casting in Java and perhaps wanting to know what type the following yields :
double d = 5.0
float f = 6.0
getInstance(d + f).getClass().toString();
would be a (painful, but workable) way to do this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primitive type "introspection"..
 
Similar Threads
java 128 bit string value and left shift
i18n Q
Primitive vs Wrapper type in Maping file
Autoboxing
Difference between int and Integer