Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes How to use Class.getMethod() by primitive type parameter? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to use Class.getMethod() by primitive type parameter?" Watch "How to use Class.getMethod() by primitive type parameter?" New topic
Author

How to use Class.getMethod() by primitive type parameter?

fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
I have to use Class.getMethod('methodname',paramA) to load a 'methodname(float)' method,paramA={new Float(13.0)},but when run it, throw a nosuchmethod exception,how JVM distinguish the primitive type parameter and its wrapper?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
you have to use the float.class (not Float.class) when looking up primitive methods that take floats.
Other primitives follow the same format:
int.class, double.class, boolean.class, etc. The only problem with this is that I don't think that it's possible to dynamically determine these class names (i.e., you can't pass in a String and do a Class.forName() on it); instead, you have to have a bunch of if/elses to test the string.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
thanks,it work.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's an exercise I did in reflection, inspired by a magazine article with a great idea and code that made me wince. You might step through ThingTester with a debugger and see how it handles int and Integer parameters. I guess I should post a ZIP of the source as well as the HTML version. Something to do tomorrow night. (Kill Bill tonite!) (Next day: Kill Bill rocks!)
Interpreter
[ October 14, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to use Class.getMethod() by primitive type parameter?
 
Similar Threads
Acessing a class method
Frank Carver help me with this NaN Question.
Introspection - Class object representing an array?!
Float a=3 doesn't compile but Short a=3 does. Why ?
int to float conversion