jakarta-bcel-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Huw Evans <...@dcs.gla.ac.uk>
Subject Re: [OT - sort of] Primitives hurt!
Date Sat, 18 Jan 2003 18:18:21 GMT

> 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:   <mailto:bcel-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:bcel-user-help@jakarta.apache.org>


Mime
View raw message