Course contentsShow
← Java
Lesson 2329 of 2,87045. Reflection APIFree lesson

Primitive Types and Array Classes

Understand how primitive types like int.class work and how arrays have their own Class objects like int[].class.

Primitive Types and Array Classes

You'll learn: How Java's reflection system represents primitive types and arrays through special Class objects.

The Surprising Truth About Primitives

Even though primitive types like int, double, and boolean aren't objects in normal Java code, the reflection API still gives them Class representations. You access these using the .class literal syntax on the primitive type name itself:

Class<?> intClass = int.class;
Class<?> booleanClass = boolean.class;
Class<?> charClass = char.class;

These Class objects let you identify and work with primitives in reflective contexts, like when inspecting method parameters or field types.

Think of it like this: Even though a hammer isn't a tool catalog entry, the catalog still has a page describing what a hammer is. Similarly, primitives get their own Class descriptor.

Arrays Get Their Own Class Objects Too

Every array type in Java—whether primitive or reference—has its own distinct Class object:

Class<?> intArrayClass = int[].class;
Class<?> stringArrayClass = String[].class;
Class<?> twoDimArray = int[][].class;

These array Class objects are different from their element types. int[].class is not the same as int.class, just as a box of apples isn't the same as an apple.

You can even inspect arrays reflectively:

Class<?> arrayClass = int[].class;
System.out.println(arrayClass.isArray());        // true
System.out.println(arrayClass.getComponentType()); // int

Why This Matters

When using reflection to inspect methods, fields, or constructors, you'll encounter these Class objects representing primitives and arrays. Recognizing them helps you handle all Java types uniformly through the reflection API.

Key Takeaway: Primitives like int.class and arrays like int[].class have their own Class objects in Java's reflection system, allowing uniform type inspection across all Java types.