6

In Java it is valid to have a class literal for primitives like int.class. Was this allowed even prior to the introduction of auto boxing feature in Java ? What does the object that we get out of int.class contain actually ? Why is it valid ?

2 Answers 2

3

int is not a class, so int.class doesn't really make sense.

The reason is they went cheap, and used one Class to represent all types. So for example, Method.getReturnType() returns a Class, which could actually represent a class, or a primitive, an array, or even void.

That was sort of fine, before generics, since there weren't too many kinds of types. After generics, things get really messy, the new Type hierarchy makes even less sense, since it incorporates the old messy Class; as a result the tree of Types looks nothing like the tree of types in the language spec.

Sign up to request clarification or add additional context in comments.

Comments

3

yes, it is a key part of reflection (e.g. describing a method which takes an int parameter). there is a Void class as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.