Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a class literal in Java?

Tags:

java

literals

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.

To what type of variable can this literal be assigned to?

Please give a small example if possible.

like image 984
gameover Avatar asked Jan 29 '10 08:01

gameover


People also ask

What is the class literal?

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 .

What are literals in Java?

Literal in Java is a synthetic representation of boolean, numeric, character, or string data. It is a means of expressing particular values in the program, such as an integer variable named ''/count is assigned an integer value in the following statement. int count = 0; A literal '0' represents the value zero.

What is the type of the class literal Foo class?

The non-erased type of Foo. class would be java.

How many literals are there in Java?

There are 4 types of integer literals in Java: binary (base 2) decimal (base 10) octal (base 8)


1 Answers

Class<String> c = String.class; 

Check out the Javadoc for java.lang.Class to see what you can do with one of these little guys - mostly related to reflection

like image 128
Steven Schlansker Avatar answered Oct 02 '22 19:10

Steven Schlansker