一、概述
(1)String java.lang.Class.getName()
Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
返回代表这个类的实体(类、接口、数组类、原始类、或空类)的名字,比如字符串。
getName()返回的是虚拟机里面的class的表示,jni中使用的就是这种表示方式。对于基本类型的数组,会显示[加上对应字符的代号;其他类型的数组,会在数组的类名前加上[L。对于内部类用$分隔,如果是匿名内部类的对象会用$1、$2、$3……以此类推这样的表示。
(2)String java.lang.Class.getCanonicalName()
Returns the canonical name of the underlying class as defined by the Java Language Specification. Returns null if the underlying class does not have a canonical name (i.e., if it is a local or anonymous class or an array whose component type does not have a canonical name).
返回由Java语言规范定义的基础类的标准名。如果基础类没有一个标准名(也就是说,如果它是一个本地或匿名类或者是一个数组,并且其组件类型没有一个标准名),则返回空。
getCanonicalName()返回的是更容易理解的表示方式。对于数组会在数组的类名之后加上[]。对于匿名内部类的对象会返回null。
二、区别
对于普通类来说二者并无区别,都是“包名.类名”的形式。
对于数组来说区别就大了,比如byte[]类型,前者就是[B,后者就是byte[],比如byte[][]类型,前者就是[[B,后者就是byte[][]。
三、代码验证
package com.demo.test;
import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
public class ClassDemo {
/*
如果不生成静态内部类会报错
No enclosing instance of type ClassDemo is accessible.
Must qualify the allocation with an enclosing instance of type ClassDemo (e.g. x.new A() where x is an instance of ClassDemo).
*/
public static abstract class AnonymousInnerClass {
public abstract void anonymousMethod();
}
public static void main(String[] args) {
Class<?>[] className = {
Boolean.class,//java.lang.Boolean
Character.class,//java.lang.Character
Byte.class,//java.lang.Byte
Short.class,//java.lang.Short
Integer.class,//java.lang.Integer
Long.class,//java.lang.Long
Float.class,//java.lang.Float
Double.class,//java.lang.Double
Void.class,//java.lang.Void
Object.class,//java.lang.Object
//Class<?>.class,//Syntax error on token(s), misplaced construct(s)
List.class,//java.awt.List
ArrayList.class,//java.util.ArrayList
Vector.class,//java.util.Vector
Set.class,//java.util.Set
HashSet.class,//java.util.HashSet
Map.class,//java.util.Map
HashMap.class,//java.util.HashMap
//如果是非数组类,会输出 “包名.类名”
ClassDemo.class,//com.demo.test.ClassDemo
//如果是内部类,会输出 “包名.类名.内部类名”
AnonymousInnerClass.class,//com.demo.test.ClassDemo.AnonymousInnerClass
//如果是匿名内部类,会输出null
(new AnonymousInnerClass() {
@Override
public void anonymousMethod() {
}
}).getClass(),//null
boolean.class,//java.lang.Boolean
char.class,//java.lang.Character
byte.class,//java.lang.Byte
short.class,//java.lang.Short
int.class,//java.lang.Integer
long.class,//java.lang.Long
float.class,//java.lang.Float
double.class,//java.lang.Double
void.class,//java.lang.Void
//object.class,//object cannot be resolved to a type
(new Boolean[1]).getClass(),//[Ljava.lang.Boolean;
(new Character[1]).getClass(),//[Ljava.lang.Character;
(new Byte[1]).getClass(),//[Ljava.lang.Byte;
(new Short[1]).getClass(),//[Ljava.lang.Short;
(new Integer[1]).getClass(),//[Ljava.lang.Integer;
(new Long[1]).getClass(),//[Ljava.lang.Long;
(new Float[1]).getClass(),//[Ljava.lang.Float;
(new Double[1]).getClass(),//[Ljava.lang.Double;
(new Void[1]).getClass(),//[Ljava.lang.Void;
(new Object[1]).getClass(),//[Ljava.lang.Object;
(new Class<?>[1]).getClass(),//[Ljava.lang.Class;
//(new Class<? extends Boolean>[1]).getClass(),//Cannot create a generic array of Class<? extends Boolean>
//(new List<Boolean>()).getClass(),//The type List is not generic; it cannot be parameterized with arguments <Boolean>
(new ArrayList<Boolean>()).getClass(),//java.util.ArrayList
(new Vector<Integer>()).getClass(),//java.util.Vector
(new HashSet<Double>()).getClass(),//java.util.HashSet
(new HashMap<String, Float>()).getClass(),//java.util.HashMap
(new ClassDemo[1]).getClass(),//[Lcom.demo.test.ClassDemo
(new boolean[1]).getClass(),//[Z
(new char[1]).getClass(),//[C
(new byte[1]).getClass(),//[B
(new short[1]).getClass(),//[S
(new int[1]).getClass(),//[I
(new long[1]).getClass(),//[J
(new float[1]).getClass(),//[F
(new double[1]).getClass(),//[D
//(new void[1]).getClass(),//void[] is an invalid type
//(new object[1]).getClass(),//object cannot be resolved to a type
//(new class<?>[1]).getClass(),//Syntax error on token "class", invalid Type
};
for(int i=0;i<className.length;i++) {
//Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
System.out.println(className[i].getName());
}
System.out.println("============================");
for(int i=0;i<className.length;i++) {
//Returns the canonical name of the underlying class as defined by the Java Language Specification. Returns null if the underlying class does not have a canonical name (i.e., if it is a local or anonymous class or an array whose component type does not have a canonical name).
System.out.println(className[i].getCanonicalName());
}
}
}
四、运行截图