---
title: "Autoboxing and Wrapper Classes in Java"  
description: "J2SE 5.0 introduced a new feature called autoboxing and unboxing that automatically converts between the primitive data types and their wrapper classe"  
author: "Jonas Stuart"  
published: 2016-05-26  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11129/autoboxing-and-wrapper-classes-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Autoboxing and Wrapper Classes in Java

J2SE 5.0 introduced a new feature called **autoboxing**and **unboxing**that automatically converts between the primitive data types and their [wrapper classes](https://www.mindstick.com/articles/12133/wrapper-classes-constructors-and-methods). To understand this feature and appreciate its [importance](https://www.mindstick.com/articles/13099/the-importance-of-machine-learning-service-providers), we need to understand the previously used wrapper classes.

##### Wrapper Classes

As we know, Java is highly [object oriented](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s). But what about its primitive data types? Are these objects? The answer to this question is no; the primitive data types in Java are not classes. Therefore, we lose the [advantages](https://www.mindstick.com/articles/12841/5-advantages-of-customer-portal-you-didn-t-know-about) that we have with classes when using the primitive data types.

**Here are some of the [disadvantages](https://www.mindstick.com/blog/300577/advantages-and-disadvantages-of-web-3-0):**

- The simple data types are not part of the Object hierarchy and therefore cannot be used as objects, as we would do with any other class in the Object hierarchy.

- We cannot pass a primitive data type to a Java method by reference; it is always sent by value.

- Two different methods in our program cannot refer to the same instance of a simple data type.

- Some classes can use only objects and cannot use simple data types. For example, the Vector class we covered previously cannot hold a list of numbers.

To overcome this and other [limitations](https://www.mindstick.com/interview/121/what-are-the-benefits-and-limitations-of-using-hidden-fields), Java provides type wrappers for all its primitive data types.

- The Integer class wraps an int data type,

- the Float class wraps a float data type,

- and so on…..

Each of the primitive data types is wrapped into a class having the same name as the data type but with the first letter capitalized. The [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) are char, for which the [wrapper class](https://www.mindstick.com/interview/1939/what-are-wrapper-classes-in-java) is called Character, and the int type, for which the wrapper class is Integer. All these wrapper classes are derived from the Number class.

##### Important Fields and methods provided by Wrapper Classes

Let’s go over a few important fields and methods of these wrapper classes that we will use frequently.

- The MAX_VALUE and MIN_VALUE fields define the maximum and the minimum values for the data type being wrapped.

- The parseInt method (and the parseXxx methods for other data types) takes a string argument with an optional radix argument and returns the corresponding data type to the caller after converting the value specified in its argument.

- The valueOf method takes a primitive data type as its argument and returns an object of the corresponding wrapper class.

- The [toString method](https://www.mindstick.com/forum/160331/what-is-the-tostring-method-in-javascript-and-how-can-it-be-used-for-type-conversion) returns the string representation of the value of the wrapped primitive data type.

- These classes also provide a xxxValue() method that returns the wrapped primitive type.

For example, the **booleanValue** method of the class Boolean will return a boolean data type, and the intValue method of the class Integer will return an int variable.

For simple tasks, **primitives** are easy to use because we can use many of the operators on them, rather than calling methods.

##### Here are some features of these wrapper classes worth noting:

- All the methods of the wrapper classes are static

- A wrapper class does not contain constructors

- And the objects of the wrapper classes are immutable, which means that once a value is assigned to a wrapper class object, it cannot be changed.

---

Original Source: https://www.mindstick.com/blog/11129/autoboxing-and-wrapper-classes-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
