Yes. Sometimes your data is an int
, a primitive, but you need the data in an object.
For example, a method might require an object for a parameter, but what you have is an int
.
There is a division between primitive data and objects. The division sometimes needs to be crossed. For each primitive type, there is a corresponding wrapper class. A wrapper class is used to put primitive data value into an object. Think of wrapping up the data with an object, like wrapping up a gift.
Wrapper classes can also go the other direction.
Objects (of a wrapper class) can be converted into primitive data.
The table shows primitive types and their wrapper classes.
Java is case sensitive,
so byte
and Byte
are different types.
As an example, the value 103 could be held in 32 bit section of memory
that is of primitive data type int
.
The same value could be held in an object that is of type Integer
.
The object will use many more than 32 bits.
The wrapper classes are defined in the package java.lang
and so (like all classes in that package) are automatically available to your programs.
primitive type | Wrapper type |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Is String
a wrapper class?