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
- 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).
- The classes in java.util package handles only objects and hence wrapper classes help in this case also.
- Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
- An object is needed to support synchronization in multithreading.
Primitive Data types and their Corresponding Wrapper class
Autoboxing and Unboxing
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.
Values of Wrapper objects (printing as objects) Byte object byteobj: 1 Integer object intobj: 10 Float object floatobj: 18.6 Double object doubleobj: 250.5 Character object charobj: a Unwrapped values (printing as data types) byte value, bv: 1 int value, iv: 10 float value, fv: 18.6 double value, dv: 250.5 char value, cv: a
Related Article :
Comparison of Autoboxed Integer objects in Java
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Primitive Wrapper Classes are Immutable in Java
- Utility methods of Wrapper classes | valueOf(), xxxValue(), parseXxx(), toString()
- Nested Classes in Java
- Classes and Objects in Java
- Abstract Classes in Java
- Understanding Classes and Objects in Java
- Access specifiers for classes or interfaces in Java
- Private Constructors and Singleton Classes in Java
- Parent and Child classes having same data member in Java
- Compute modulus division by a power-of-2-number using Wrapper Class
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
Improved By : PasinduSiriwardana