Wrapper classes

Monday, November 6, 2023

In Java, wrapper classes are a set of classes that encapsulate primitive data types. These classes allow primitive data types to be treated as objects, which is useful in situations where working with objects is required instead of primitive types. In this section, we will explore wrapper classes in Java and how they are used.

Common Wrapper Classes

Each primitive data type in Java has a corresponding wrapper class. Here are some of the most common wrapper classes:

  1. Integer: Wraps the int data type.
  2. Double: Wraps the double data type.
  3. Boolean: Wraps the boolean data type.
  4. Character: Wraps the char data type.
  5. Byte: Wraps the byte data type.
  6. Short: Wraps the short data type.
  7. Long: Wraps the long data type.
  8. Float: Wraps the float data type.

Advantages of Wrapper Classes

Wrapper classes offer several advantages in Java programming:

  1. Objects Instead of Primitives: They allow primitive data types to be treated as objects, making it easier to use them in data structures and collections of objects.

  2. Useful Methods: They provide useful methods for performing operations on primitive values, such as conversion to strings, comparisons, and more.

  3. Null Values: Wrapper classes can have a null value (null), which is useful for representing the absence of a value.

Declaration and Use of Wrapper Classes

To declare and use wrapper classes, simply create an instance of the corresponding wrapper class and initialize it with a primitive value.

Here is an example:

Integer integerNumber = new Integer(42);
Double decimalNumber = new Double(3.14);
Boolean isTrue = new Boolean(true);
Character character = new Character('X');

Java also provides a feature called "autoboxing" that allows automatic conversion between primitive types and their wrapper classes.

For example:

Integer integerNumber = 42; // Autoboxing
int value = integerNumber; // Autounboxing

Converting Between Primitive Data Types and Wrapper Classes

You can convert between primitive data types and their wrapper classes using methods provided by the wrapper classes.

For example, to convert an Integer to int, you can use the intValue() method:

Integer integerNumber = new Integer(42);
int value = integerNumber.intValue();

Usage Examples

Wrapper classes are especially useful when working with collections of objects, such as lists and maps, which require elements to be objects rather than primitive types. They are also useful in situations where a null value is needed.

Performance Note

Using wrapper classes can have a slight impact on performance and memory consumption compared to primitive types. Therefore, it is recommended to use primitive types whenever possible and resort to wrapper classes only when necessary.

In summary, wrapper classes in Java are a valuable tool that allows you to work with primitive data types as objects. These classes offer flexibility and additional functionality in Java programming.

Share it

Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.


The code has been successfully copied to the clipboard.

More Exercises

Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!



Variables are an essential component in any programming language, and Java is no exception. In this post, we will explore the concept of variables in Java and how they are used in programming.


Fundamentals of data types

Data types are a fundamental concept in programming and play a crucial role in Java, a strongly typed programming language. In this section, we will explore the basic concepts of data types in Java and their importance in application development.


Primitive data types

Primitive data types in Java are fundamental elements that represent simple values and are used to store basic data.


Wrapper classes

In Java, wrapper classes are a set of classes that encapsulate primitive data types.


Reverse chars

Write a program to ask the user for three letters and display them in reverse order.


Double value

Write a Java program that requests the width (x) and height (y) of a rectangle and calculate the perimeter, area and diagonal.


Float value

Create a program Java to ask the user for a distance in meters and the time taken (hours, minutes, seconds).


Generate a random number

Create a Java program that prompts the user for 2 integers and displays a random number on the screen between those values.

Data types

Practice with easy and intermediate Java exercises on different types of text, numeric or boolean data.

Fundamentals concepts
Practice exercises

Keep learning

C# Programming Course

Free programming course with practical exercises and solutions in C#. Start learning right now!

Exercises C# App

Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play