Type Wrappers

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Yes. Sometimes your data is an integer, a primitive, but part of the program requires an object.

Type Wrappers

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 can convert a primitive data value into an object. Objects (of the correct 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.

primitive typeWrapper type
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

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.


QUESTION 18:

Is String a wrapper class?