what are wrapper classes in javause of wrapper class in javawrapper class methods in javawhy we use wrapper class in javatypes of wrapper class in javawrapper class in java javatpointadvantage of wrapper class in javacustom wrapper class in java

Last updated 28. January 2021 from

A class that wraps a primitive data type into an object is called a wrapper class in Java.

Simply put: A wrapper class provides a mechanism to convert a value from a primitive data type to an object and vice versa.

For example, int is converted to the Integer class, double to the Double class, and char to the Character class.

When we create an object of the wrapper class, it contains a variable in which we store the value of the primitive data type. Simply put: We can pack the value of a primitive type into an object of the packing class.

If we are. For example, if you create an object of the Integer class, it contains a single variable (or field) that stores an int value of 25, as shown in the following figure. Thus, an integer is an enveloping class of the data type int.

Code for unpacking the int-value

Envelope value :

int x = 25;
Integer iWrap = new Integer(x); // Passes a primitive type to the envelope constructor.

The deployment of cash value :

int unWrapped = iWrap.intValue() ;

Why do we need envelope classes in Java?

There are several reasons to use envelope classes in Java, listed below:

1. We have seen several applications on the Internet that receive data from the user and send it to the server. For example, in a business application like Amazon, we enter information like name, debit or credit card number, address, phone number, etc. when we buy something.

All this data is sent to the server. The server is waiting for this data in the form of objects. In our data – the name is an object of type string, but the debit card number is a value of type int, i.e. not an object.

This value of the primitive data type must be converted into an object before being sent to the server. To overcome this conversion, we need Java wrapper classes.

The wrapper class contains a variable that stores the value of the primitive data type. When we create an object of the wrapper class, it carries the value of a primitive data type.

The object is then sent to the server. The server receives a primitive type value of the object and uses it.

2. For Java collections (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.), the collection classes are defined in the java.util package, which stores only objects, and not values of the primitive type.

If we want to use collection classes with primitive type values, we need to convert them into objects.

For example, primitive data types cannot be stored directly in the ArrayList array because the elements of the ArrayList array must be objects.

There is no such thing as a massive list. Here double is a primitive data type, but not an object. This is why ArrayList is not compiled.

To convert a value of a primitive type into an object, the Java platform contains wrapper classes in the Java API. It provides a convenient way to include or envelop a primitive data type in an object.

3. Java synchronization works with multithreaded objects.

4. Since Java only supports value-based calls, if we pass a primitive value, the original value will not be changed. But if we want to change the original value, we need to convert the value of the primitive type into an object, which can be done with an envelope class.

5. To perform serialization in Java, we need to convert the objects into streams. If we have a primitive value, we can convert it into objects using Java wrapper classes.

6. There are several auxiliary classes defined in the java.util package that work with objects.

Message: Java 5.0 introduced two important new features, autoboxing and deboxing, which automatically convert datatype values to objects and objects to datatype values.

The process of automatically converting a primitive data type into an object is called autoboxing and conversely unboxing.

Look at the source code and make sure that autoboxing and unboxing are automatically done by the Java compiler when we want to create an ArrayList with an int value.

// With Autobox (Java version 5.0 and higher)
ArrayList ar = new ArrayList(); // Integer ArrayList.
ar.add(3) ;
int num = ar.get(0) ;

JavaList of envelope classes

The Java platform provides a list of eight envelope classes for each of the primitive types. These wrapper classes are stored in the java.lang file.

All Java envelope classes are immutable and final, every object has a primitive value. Immunity means that objects, once created, cannot change their internal values. The packing class methods are implicitly definitive and cannot be changed.

They are useful for converting primitive data types into object forms. They are listed in the following table:

Table: Primitive date types and their corresponding envelope classes

Primary data type Corresponding packing class
Symbol The sign
Byte Byte
short side In short
int Complete
long Long
Swimming Smoother
duplicate Double
Sync and corrected by dr.jackson for Bulev

Naming convention : Java envelope class names begin with capital letters. Most of the wrapper class names match the names of the primitive data types, with the exception of Integer and Character.

An object of the Boolean class encapsulates a Boolean value of true or false. We use ArrayList to store duplicate values in an ArrayList array.

Envelope classes Example programs

1. Let’s take the example of a program where we explicitly want to convert a primitive value of data of type int into an integer object. The Java compiler also converts int to integers automatically. It’s called an autobox. Please refer to the source code for a better understanding.

Program source code 1 :

public class Wrapping [
public static void main(String[] args)
[
int a = 50 ; // Value of the primitive datatype.

Integer i = Integer.valueOf(a); // Here we explicitly convert int to integer.
Integer j = a; // Autoboxing takes place here. The Java compiler will internally write Integer.valueOf(a).

System.out.println(a++i++j);
}
}

The valueOf() method of the Integer class is used for the number int in the Integer object. The return type of this method is the object. This method is static in nature. So we call it by the name of the class.

2. We can also convert from integral to int explicitly and implicitly by the compiler. Let’s write the code for that.

Program source code 2 :

public class Unwrapping [
public static void main(String[] args)
[
// To convert the class Integer to int, create an object of the class Integer and pass the value to the constructor.
Integer a = new integer(50) ;

int i = a.intValue();// Here we explicitly convert an integer to int
int j = a; // Unpacking takes place. The Java compiler will write a.intValue() internally.

System.out.println(a++i++j);
}
}

The intValue() method of the Integer class is used to convert an integer object into a primitive value of type int. It’s called unpacking. The return type of this method is int.

Program source code 3 :

public class WrappingUnwrapping [
public static void main(String[] args)
[
char ch=a; // datatype char.
Character chrobj = new character (ch); // pack the character value into the character object.

Byte a = 10; // Value of the data type byte.
Byte byteobj = new byte(a); // envelope value of the byte type in the byte object.

int b = 20; // Value of type int.
Integer inbj = new Integer(b); // Packs a value of type int into an integer object.

float c = 18.6f; // Value of the float type.
Float floatobj = new Float(c); // Packs a value of type Float into a Float object.

double d = 250.5; // Double d = 250.5; // Value of the data type double.
Double doubleobj = new Double(d); // A value of data type Double is wrapped in a Double object.

// Show the values of the objects in the packing class.
System.out.println(Display the values of the objects of the class envelope:);
System.out.println(Drawing object: + chrobj);
System.out.println(Byte object: + byteobj) ;

System.out.println(Integer object: + inbj);
System.out.println(Floating object: + floatobj);
System.out.println(Double object: + doubleobj) ;

System.out.println(n);
// Get the values of the primitive data types of the objects.
// Decompose the objects into values of type primitive data.
char chr = chrobj ;
by = byteobj ;
int in = intobj ;
float fl = floatobj ;
double db = doubleobj ;

// Display the values of the data types.
System.out.println(Show extended values: );
System.out.println(char value: + chr);
System.out.println(byte value: + by) ;

System.out.println(int value: + in);
System.out.println(floating value: + fl);
System.out.println(double value: + db);
}
}

Go away:
Display of the values of the objects in the packing class :
Drawing Object :
Byte object : 10
The whole object : 20
Floating object : 18,6
Dual-Use : 250.5

Display extended values :
Chart value :
Byte value : 10
Int : 20
Floating value : 18.6
Dual Value : 250.5

Key points:

1. A Java wrapper class provides various constructors, constants, and conversion methods for manipulating different values of primitive data types.

2. There are no manufacturers in the packaging classes.

3. The symbol class does not have a constructor with a string as parameter.

4. The appearances of all packaging classes are invariable and definitive. This means that once an object is created, its internal value cannot be changed.

I hope this tutorial has covered the important points regarding envelope classes in Java with example programs. I hope you understood and enjoyed this topic.

If you like this tutorial, please share it on social media.
Thank you for reading !!!!

what are wrapper classes in javause of wrapper class in javawrapper class methods in javawhy we use wrapper class in javatypes of wrapper class in javawrapper class in java javatpointadvantage of wrapper class in javacustom wrapper class in java

You May Also Like

How To Fix Thumbnails Not Showing In Windows 10

The Windows thumbnail function makes it easy to recognize photos and movies…

A Complete Guide On Resolving The Issue of Phone Overheating

The phone can sometimes even get hot, whether it’s during a game…

How To Fix Windows 10 File Explorer Dark Theme Not Working

When it comes to customisation options, Windows 10 offers users a considerable…

Apex Legends Crashing PC Fix 2020

Various problems can occur during gambling. If you’re playing the new royal…