1. 程式人生 > >Java中的類文字(Class literal)是什麼?

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