Home  >  Article  >  Java  >  Wrapper Class in Java

Wrapper Class in Java

王林
王林Original
2024-08-30 16:00:11724browse

Wrapper Class is an important class of java.lang library. Wrapper class objects create a wrapper for the primitive data types. While creating an object of the wrapper class, space is created in the memory where primitive data type is stored. Wrapper class provides some features for the conversion of the object to primitive data & primitive data to object, i.e. boxing/unboxing. Conversion from objects to primitive data & primitive data to an object takes place automatically. Primitive data type refers to the int, float, char, double, byte, etc.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax: 

Below given declaration shows how a Wrapper class works in the java program.

Example:

int i = 100;

In the below-given example, we can see i is an integer data type. Sometimes in the java integer needs to be passed as a type of object. In this case, we can use the wrapper class to convert an integer into an object.

Code:

Integer intVal = new Integer(i);

In the above-given syntax, we can see how a primitive data type is being converted to an object using an Integer class object. Also, we can say primitive data type is wrapped as an object.

Uses of Wrapper Class in Java

Given below are few uses of the wrapper class:

  • Wrapper Class can be used to convert an object into a primitive data type or its vice-versa.
  • The use of wrapper classes improves the performance of the program.
  • Wrapper class helps in the serialization of objects & its vice versa. It can convert primitive data to objects. Sometimes we need to stream objects; in that case, the wrapper class can convert them into serialization format.
  • Some important methods are provided by the wrapper class, which is used to perform the searching & sorting in the collections.
  • Subtraction & addition, these types of operations cannot modify the old value of the Wrapper class primitive integer; that’s why the Wrapper class is known as immutable.
  • Wrapper class is used in the multithreading process, as the multithreading process needs an object for synchronizing processes. The wrapper class converts different data types to an object.

On the basis of JavaAPI, the Wrapper class hierarchy keeps Object at the top of the different primitive classes. Number, Character & Boolean comes at the second level just after the Object. Byte, Short, Int, Long, Float, Double come under the Number data type at the third level.

Wrapper classes use the following two mechanisms Autoboxing & unboxing, for the conversion/wrapping of the data type or conversion of an object into the primitive data type.

  • Autoboxing: Autoboxing refers to the automatic conversion of the primitive data type to object using Wrapper classes. It is known as Autoboxing. In the below-given Examples, int converted to Integer object & in examples c, a different primitive data type is being converted to the corresponding object.
  • Unboxing: Unboxing is the reverse process of Autoboxing. Automatically converting wrapper class objects to the corresponding primitive data type is known as Unboxing. In the below-given example b, the Integer object is being converted to an int primitive data type.

Examples of Wrapper Class in Java

Below are the different examples of Wrapper Class in Java:

Example #1

In the below-given example, we can see how manual conversion takes place through wrapper class from int i to an object k.

Code:

import java.util.*;
class WrapperExample {
public static void main(String args[]){
int j=100;
//converting int j to integer k as an object
Integer k = new Integer(j);
System.out.println(j + "\n" + k);
}
}

Output:

Wrapper Class in Java

In the above-given example, we can see how conversion takes place explicitly.

Example #2

In the below-given example, we can see this process of conversion sometimes takes place automatically, i.e. known as autoboxing.

Code:

import java.util.*;
class AutoboxingUnboxingExample {
public static void main(String args[]){
int j = 500;
ArrayList<Integer> arrValues = new ArrayList();
arrValues.add(j);  // autoboxing takes place implicitly
System.out.println(arrValues.get(0));
}
}

Output:

Wrapper Class in Java

In the above-given example, the int value is converted to an object implicitly as an object. Further, this value can get from the ArrayList.

Example #3

In this example, we will go through the implementation of Unboxing. Unboxing is the reverse process of Autoboxing.

Code:

import java.util.*;
class AutoboxingUnboxingExample {
public static void main(String args[]){
ArrayList<Integer> arrValues = new ArrayList();
arrValues.add(250);
//unboxing here as int data type from Integer object
int k = arrValues.get(0);
//value printed is in primitive data type
System.out.println(k);
}
}

Output:

Wrapper Class in Java

In the above-given example, the ArrayList object field is converted into k primitive data type, i.e. int k.

Example #4

The following given example have all the details of Autoboxing & Unboxing.

Code:

import java.util.*;
class WrapperConversionExample {
public static void main(String args[]){
int i = 15;
float j = 9.6f;
double k = 120.8;
byte l = 1;
//creating instance of Integer object
Integer iObj = new Integer(i);
//creating instance of Float object
Float fObj = new Float(j);
//creating instance of Double object
Double dObj = new Double(k);
//creating instance of Double object
Byte bObj = new Byte(l);
//value printed is in object
System.out.println("Value as an Integer object > " + iObj);
System.out.println("Value as a Float object > " + fObj);
System.out.println("Value as a Double object > " + dObj);
System.out.println("Value as a Byte object > " + bObj);
//primitive data type from the object
int m = iObj;
float n = fObj;
double o = dObj;
byte p = bObj;
//value printed is in primitive data type
System.out.println("Value as an int primitive type > " + m);
System.out.println("Value as a float primitive type > " + n);
System.out.println("Value as a double primitive type > "+ o);
System.out.println("Value as a byte primitive type > " + p);
}
}

Output:

Wrapper Class in Java

In the above-given program, we can see the implementation of Wrapper classes. Wrapper classes are converting the primitive data type to object & object to the primitive data type. The wrapper class provides separate classes for each primitive data type.

Conclusion

Through the Wrapper classes, we can easily understand autoboxing & unboxing how conversion takes place from primitive to object & its vice versa, which can be easily understood through Wrapper classes. For each of the primitive data types, there is a dedicated class in java.

The above is the detailed content of Wrapper Class in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Singleton Class in JavaNext article:Singleton Class in Java