Trusted answers to developer questions

What are wrapper classes in Java?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

A wrapper class in Java converts a primitive data type into class object.

Java is an object-oriented language that only supports pass by value. Therefore, wrapper class objects allow us to change the original passed value. These wrapper classes help with multithreading and synchronization because, in Java, multithreading only works with objects. Moreover, most collections (like Vector, LinkedList, etc.) work with objects.

svg viewer
Primitve Data Type Wrapper Class
int Integer
short Short
byte Byte
char Character
long Long
float Float
double Double
boolean Boolean

Autoboxing

The automatic conversion of primitive data types into its wrapper class objects is known as autoboxing.

class Autoboxing {
    public static void main( String args[] ) {
     int a = 15; // Primitive data type
     Integer I = a; // Autoboxing will occur internally.
    }
}

Unboxing

The automatic conversion of wrapper class objects into its primitive data types is known as unboxing.

class Autoboxing {
    public static void main( String args[] ) {
      Integer a = new Integer(15); // Wrapper class object
      int I = a;// Unboxing will occur internally.
    }
}

RELATED TAGS

java
basics
wrapper
class
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?