6
votes

I am reading a book on Java, and they seem to use the two terms "primitive class" and "primitive data type" interchangeably.

What's the difference between the two? I understand that Integer is a wrapper class, and people reference int as a primitive data type. So is it also a primitive class?

2
a primitive class is a wrapper class for a primitive type. So for int you have Integer, for double you have Double and so on - Lino
Sounds a bit oxymoronic to me... - assylias
I don't think I've heard "primitive class" before, and if I did, my first thought would be "huh?" To my knowledge, the JLS and official Java tutorials don't call anything a primitive class. As @Lino wrote, your book probably means what most people (and the JLS) refer to as a wrapper class. - yshavit
out of curiosity, what book is that? - user85421
It's a study guide resource my school provided to study for an intro to Computer Science exam, don't know why I said it was a book. - Luke Thistlethwaite

2 Answers

5
votes

They're confusing their vernacular here.

A primitive is a data type which is not an object. int, float, double, long, short, boolean and char are examples of primitive data types. You can't invoke methods on these data types and they don't have a high memory footprint, which is their striking difference from classes.

Everything else is a class (or class-like in the case of interfaces and enums). Pretty much everything that begins with an upper-case letter, like String, Integer are classes. Arrays also classify as not-primitives, even though they may hold them. int[] isn't a primitive type but it holds primitives.

The only thing that could realistically come close would be the wrapper classes, as explained by the JLS, but even then, they're still classes, and not primitives.

4
votes

Primitive class has a special meaning in the context of reflection APIs: when you need to retrieve a method that takes a parameter of primitive type, you need a primitive class object that corresponds to that primitive type.

This is important if you must distinguish between overloads that take primitives and overloads that take wrappers:

void someMethod(int n);
void someMethod(Integer n);

There are two ways to obtain this class object:

  • Using class literal, e.g. int.class, or
  • Using TYPE member of the corresponding wrapper class, e.g. Integer.TYPE.

This is not the same class as the class representing the primitive wrapper. In other words,

int.class != Integer.class