• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to construct a Class object to represent a primitive type?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to use the Class.getDeclaredMethod(String name, Class[] parameterTypes) to get a method from a class. Here parameter name is the name of the desired method and parameterTypes parameter is an array of Class objects that identify the method's formal parameter types.
I have trouble in building such an Class array. For a class or interface type, it's no problem. We can use class.forName to construct a Class object. But for a primitive type such as int and long, I don't know how to do. I try to use Class.forName("int"), but fail. Could you tell me how to do it?
Thanks ahead.
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class c = int.class
can give u the class object which represents int.
karthik.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The is no way that you can get a primitive to look like an object of a class. They are two separate creatures. Objects are peices of memory on the heap, and primitives are kept separately in variables with no corresponding area on the heap. Objects have classes which have related methods in the Method Area. Primitives have no related methods.
The closest that you can do is get a class which is the wrapper class for the primitive. You can have an Integer object which holds the corresponding primitive int. There is a Long class that can hold the corresponding long value etc. Look up Double, Character, Byte etc.
 
Sheriff
Posts: 16910
283
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
Cindy,
I hope you're not saying that Karthik is wrong, because what he gave is legal (JLS �15.8.2 - Class Literals). Have to admit though, this was new to me.
Junilu
Originally posted by Cindy Glass:
> The is no way that you can get a primitive
> to look like an object of a class.
[This message has been edited by JUNILU LACAR (edited June 05, 2001).]
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WOW ... this really suprised me too...

Though in the long run... how is this really useful... you have to know that what you are testing is an int to get an int Class object back from it...

It would be really useful if it worked dynamically like the following code illustrates... unfortunately, it throws a compiler error that states : "int cannot be derederenced"



Is there any way to dynamically get a Class from a primitive reference?

-Nate
 
Junilu Lacar
Sheriff
Posts: 16910
283
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
Originally posted by Nathan Pruett:
> Though in the long run... how is this really useful...
It's useful in answering the question "Is there a method of this class that takes primitive arguments [byte | short | int | long ...]?"
> Is there any way to dynamically get a Class from a
> primitive reference?
Putting the question back to you, how would this be useful?
Junilu
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is, you can't possibly have a primitive in your program without knowing its type. Well, not without declaring its type at least. I suppose you could forget it later. So personally, I can't imagine a context where it would be useful to get the "class" (i.e. Class object) dynamically for a primitive. In the program above, foo is an int, period. A reference variable might hold some type which is a subclass of the declared type, so it's useful to find out dynamically what type you really have - but a primitive variable always holds the type it's declared.
The one time I found the int.class construct useful is exactly the same one simen needed it for - when using one of the reflection methods where you need to specify the types of the arguments to a method or constructor. You can also specify array types this way, like "byte[][].class" for an array or arrays of bytes.
[This message has been edited by Jim Yingst (edited June 05, 2001).]
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope never heard of doing it with primitives. You learn something new everyday! Must be time to brush up on reflection.
So of course I looked it up. Apparently the JVM itself is the only thing that can instantiate a Class object, and since IT doesn't have to follow the rules of the "new" operator, it does it anyway. But it can only give you the "Class" class, not the name of the class of the object itself (do I sound like I am stuttering?)


Gives you
The class of int is java.lang.Class
The class of Button[button0,0,0x0,invalid,label=] is java.awt.Button
So you are extracting a different sort of information.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's useful in answering the question "Is there a method of this class that takes primitive arguments [byte | short | int | long ...]?"



DOH! Sorry...

Putting the question back to you, how would this be useful?



Sorry again... when I first thought of it, I thought of using this for some type of overloading-type of operation... being able to pass anything into a method, then using the Class of the parameter to decide what to do... obviously this can't be done, since the parameter to the method has to have some type of datatype...

I need to look up more info on the Reflection API...

-Nate
 
Junilu Lacar
Sheriff
Posts: 16910
283
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
Yep, Reflection is pretty cool. Wouldn't be a JUnit without it...
Originally posted by Nathan Pruett:
> I need to look up more info on the Reflection API...
 
simen wu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lots of thank to everybody. I try the int.class and it works, though it seems a little bit strange.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example of using reflection to run a constructor that takes an int and then run a method that displays that int:

[This message has been edited by Thomas Paul (edited June 06, 2001).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here is the class it runs against:
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While int.class will work, there is also
Integer.TYPE which is of class "Class".
It seems like these two are equals.

Pho
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fantastic! There was a question a while ago that was asking how to distinguish objects and primitives for method signatures, and this appears to solve it.

Dave.
(now if I could just find and reply to the earlier post )
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic