You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Huw Evans <hu...@dcs.gla.ac.uk> on 2003/01/18 19:18:21 UTC
Re: [OT - sort of] Primitives hurt!
> The nice thing about the Type class is that I can hand it a "Class"
> object and get back the appropriate type.
>[snip]
> which works great for regular Classes, unfortunately it doesn't work for
> primitive types. Calls like:
>
> *
> Class.forName("int")
>[snip]
> int.class.toString() returns "int"
> int.getClass().getName() returns "int"
> *
>
> but
>
> *
> Class.forName("int").toString()
> *
>
> fails with a ClassNotFoundException.
>
> This is something that sucks about primitive type names in the Java API.
When you want to refer to the primitive type int as a class, use:
Integer.TYPE
the TYPE field is of type Class and is defined on all the primitive type
wrappers. For example:
public class A {
public static void main(String[] argv) throws Exception {
System.out.println(int.class);
System.out.println(Integer.TYPE);
}
}
both of these print "int". In fact, the int.class is substituted by my
compiler (javac 1.4.1 on a solaris 5.7 machine) in to the Integer.TYPE call.
So, you need to check the need to do something with the primitive type, and
instead of call Class.forName(<primitive type name>), you need to call the
appropriate wrapper.TYPE.
This is a pain, I agree, and it is a little sucky, I agree. However, it does
do the job :) Don't get me started on the use of primitives in Java, it's one
of my favourite rants about the language :)
Huw
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [OT - sort of] Primitives hurt!
Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
Huw Evans wrote:
> So, you need to check the need to do something with the primitive type, and
> instead of call Class.forName(<primitive type name>), you need to call the
> appropriate wrapper.TYPE.
>
Thank you for the tip, I didn't realize that was available in the
wrapper classes too. I do end up having to create a map of some sort to
get from the string representation to the actual class still. Jouzas'
code captures this issue (thank you!).
> This is a pain, I agree, and it is a little sucky, I agree. However, it does
> do the job :) Don't get me started on the use of primitives in Java, it's one
> of my favourite rants about the language :)
> Huw
>
It's just amazing to me, I bet this one little issue with
Class.classForName(...) and primitives probibly creates "hundreds of
thousands" of extra lines of replicated javacode spanning many Java
Development Projects the world over.
-Mark
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>