星期三, 十月 26, 2005

关于Class literal 的一些说明

关于Class literal 的一些说明

JLS 2.0 15.8.1 Lexical Literals 章节说明了”literal”的特定含义
A literal (§3.10) denotes a fixed, unchanging value.
A literal 表示一个固定的,不能改变的值.
因为如此我们把 literal翻译为常数
故此:我们把 Class Literal 翻译为 类常数

JLS 2.0 15.8.2 章节有如下话:
15.8.2 Class Literals(类常数)
A class literal is an expression consisting of the name of a class, interface,
or primitive type followed by a ‘.’ and the token class. The type of a class
is Class. It evaluates to the Class object for the named type (or for void)
defined by the defining class loader of the class of the current instance.
翻译如下:
类常数是有类(class),接口(class)或基本类型(primitive type)的名字接着句号”.”class
记号.

Class的类型是Class. 它相等于对应着名字标记的类型的Class对象(或者void).
这是有当前实例所定义的class Loader来定义的。

Jdk 5.0 的泛性中.class的 类常数表示方法频频使用。设计的目的,还是为了解决,在
reflection 技术中,关于使用基本类型(int,boolean….)作为参数的问题.
比如一个用int作为参数的Method通过.Class.getMethod(String methodName,Class[] param)
因为int本身不是一个Object也没有显式的定义申明,所以重载.class可以把int转换为
Class对象。
基本类型和Class之间的对应关系如下:
        boolean.class   ==   Boolean.TYPE
        char.class      ==   Character.TYPE
        byte.class      ==   Byte.TYPE
        short.class     ==   Short.TYPE
        int.class       ==   Integer.TYPE
        long.class      ==   Long.TYPE
        float.class     ==   Float.TYPE
        double.class    ==   Double.TYPE
        void.class      ==   Void.TYPE