Java中的类文字(Class literal)是什么?

类文字(class literal)

From the Java tutorial:

Finally, there’s also a special kind of literal called a class literal, formed by taking a type name and appending “.class”; for example, String.class. This refers to the object (of type Class) that represents the type itself.

那么怎么理解类文字是什么呢?

A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a . and the token class. The type of a class literal is Class. It evaluates to the Class object for the named type (or for void) as defined by the defining class loader of the class of the current instance.

类文字是一个表达式,由一个类的名称,接口,数组,primitive类型后面跟一个”.class”组成。它也是一个类,例如String.class是Class类的一个实例。 采用类文字可以得到它的java.lang.Class对象。

String.class == new String().getClass();//true

猜你喜欢

转载自blog.csdn.net/zhuochuyu7096/article/details/84206135