# Wrapper Classes in Java

<http://www.geeksforgeeks.org/wrapper-classes-java/>

![](/files/-LgDoURZyMCPldcjr8cg)

## 1. Wrapper Classes in Java

A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. In other words, we can wrap a primitive value into a wrapper class object.

#### Need of Wrapper Classes

1. They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method(because primitive types are passed by value).
2. The classes in java.util package handles only objects and hence wrapper classes help in this case also.
3. Data structures in the Collection framework, such as ArrayList and Vector, store only objects(reference types) and not primitive types.
4. An object is needed to support synchronization in multithreading.

### Autoboxing:

Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example -- conversion of int to Integer, long to Long, double to Double etc.

### Unboxing:

It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example -- conversion of Integer to int, Long to long, Double to double etc.

![](/files/-LgDoURamE420GJualci)

e.g.

```java
   byte a = 1;
    Byte byteObj = new Byte(a);

    System.out.println("byte object: "+byteObj);

    int ai  = 10;
    Integer intOb = new Integer(ai);
    System.out.println("int object: "+intOb);

    byte bb = byteObj;
    System.out.println("byte  : "+bb);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lin-4.gitbook.io/cs-notebook/wrapper-classes-in-java.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
