The most crucial foundation of every programming language is its data types, and it is the most vital element to learn for every beginner. The data type represents the type, character, and set of actions for the value that it stores. Java data types are useful in all aspects, whether creating a small program or building any application or software.
In Java, data types are classified into two types: primitive and non-primitive data types. Primitive data types are pre-specified, whereas non-primitives are defined by programmers rather than by Java. To provide some additional functionality and make the application programs uncomplicated, the concept of the Wrapper class got introduced in Java.
Let us understand in more detail the Wrapper class in Java.
Wrapper classes are predefined classes that convert received string numeric values into primitive data types and vice versa. They are used in Java. The jre/lib/rt.jar file contains a wrapper class by default and is part of the Java library. When constructing an object for a wrapper class in Java, it has a field in which we can put primitive data types.
Let’s understand Java wrapper classes via this program:
Output:
12
12.0
false
Note that the Wrapper class only supports string numeric values, and the Boolean wrapper class is added in Java 1.5.
The following are some wrapper classes in Java that are equivalent to basic data types:
Some key functions of Java wrapper classes are mentioned below:
Value Modification in Function
The “call by value” function in Java programming enables us to modify the arguments passed into a method by using objects modified from primitive data types. We can transmit the objects and modify the values if the parameter is not constant and needs to be modified.
An object is required to allow Java synchronization. It uses multi-threading to act on objects, and objects are necessary to recognize blocks in multi-threading.
The synchronized keyword in Java is used to identify synchronized blocks. An object is synchronized with this Java block. The same object’s synchronized blocks can only have one stream active at once. Until the stream inside the block leaves, all other processes that want to enter it are halted.
The object is transformed inside streams to execute serialization. The wrapper classes in Java are used to regenerate the object. The object’s class must directly or indirectly implement the Serializable interface.
The wrapper classes for the Java.util package support the utility classes, which are made to work with objects.
Collection Framework
ArrayList, Vector, HashSet, LinkedList, and other Framework collection classes in Java store only objects. These objects are helpful since they are instances of wrapper classes.
Listed below are the methods supported by the wrapper classes in Java:
Implementing a wrapper class is straightforward. First, create a new class that extends the desired wrapper class. For example, if you want to wrap around an InputStream, your new class would extend Java.io.InputStream. Next, add fields to your new class for any data that needs to be stored. Finally, implement the methods of the wrapper class as needed. For example, if you want your wrapper class to support reading from the underlying stream, you would need to implement the read() method of InputStream.
Wrapper classes can be extremely helpful when you need to use a lower-level language but want to take advantage of the higher-level features of Java. By creating a wrapper class, you can avoid writing complex JNI code and still get all of the benefits of using Java.
The two methods for implementing Wrapper Class in Java:
When the Java compiler automatically transforms primitive data types into the objects of their corresponding wrapper classes in Java, this is known as autoboxing. For instance, when changing an int to an integer, a char to char, a double to a double, and so on.
A primitive value is autoboxed by the Java compiler when:
For example:
//Autoboxing instance of int to Integer and char to Char
public class AutoboxSample {
public static void main(String args[]) {
char ch = ‘s’;
//primitive to Character conversion
Character s = ch;
int b = 100;
// Changing int into Integer explicitly
Integer fir = Integer.valueOf(b);
// now the compiler will compose Integer.valueOf(b)
// internally and hence doesn’t generate an error
Integer sec = b;
System.out.println(b);
System.out.println(first);
System.out.println(sec);
}
100
Explanation:
In this case, the total output is 100 as follows:
Unboxing
It is exactly the reverse of the autoboxing method. Unboxing is the process of reverting a wrapper class object to its corresponding primitive value.
The compiler uses unboxing when a wrapper class is:
//Example of unboxing changing Integer to int and Character to char
public class UnboxExam {
Character ch = ‘F’;
//Char object to primitive change
char F = ch;
Integer B = new Integer(10);
//Converting Integer to int explicitly
int fir = B.intValue();
//compiler will write B.intValue() internally
int sec = B;
System.out.println(B);
System.out.println(fir);
10
Here, the output is 10 for all the queries:
There are various benefits of wrapper classes. Listed below are the key ones:
Wrapper classes in Java are the foundation of all method invocations. It also enables Java to modify basic data types without affecting their values. Practice more and more wrapper classes in Java programs to get an understanding of wrapper classes in Java. A solid understanding of these concepts will undoubtedly help you much in your development path. For programming knowledge and more reading materials on software development, UNext is the right choice for you.
Fill in the details to know more
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
What Is The vi Editor in The Unix Operating System ?
November 25, 2022
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile