Wrapper classes Vs Primitive Data Types
Bhavishya Ambati
Craving Tech Innovation?|Chasing Excellence|Follow for Insightful Tech Trends|Connect to unlock|Student Mentor IEEESB NBKRIST|IEEE Hyderabad Section Lead18.0|LinkedIn Top ?? Voice |Active Passionate learner
What is Wrapper Class in Java?
A Wrapper class in Java is used to convert a primitive data type to an object and object to a primitive type. Even the primitive data types are used for storing primary data types, data structures such as Array Lists and Vectors store objects. Therefore, it is required to use wrapper classes for the conversion. The corresponding wrapper classes for primitive types char, byte, short and int are Character, Byte, Short, and Integer. The corresponding wrapper classes for long, float, double and boolean are Long, Float, Double and Boolean.
Wrapper Classes:
Java Autoboxing - Primitive Type to Wrapper Object
In autoboxing, the Java compiler automatically converts primitive types into their corresponding wrapper class objects. For example,
int a = 56;
// autoboxing
Integer aObj = a;
Autoboxing has a great advantage while working with Java collections.
Example: Java Autoboxing
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
//autoboxing
list.add(5);
list.add(6);
System.out.println("ArrayList: " + list);
}
}
Output
ArrayList: [5, 6]
Java Unboxing - Wrapper Objects to Primitive Types
In unboxing, the Java compiler automatically converts wrapper class objects into their corresponding primitive types. For example,
// autoboxing
Integer aObj = 56;
// unboxing
int a = aObj;
Like autoboxing, unboxing can also be used with Java collections.
Example: Java Unboxing
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
//autoboxing
list.add(5);
list.add(6);
System.out.println("ArrayList: " + list);
// unboxing
int a = list.get(0);
System.out.println("Value at index 0: " + a);
}
}
Output
ArrayList: [5, 6]
Value at index 0: 5
?A wrapper class wraps (encloses) around a data type and gives it an object appearance, this automatic conversion of primitive data types into equivalent wrapper type is known as Boxing/Autoboxing. Wherever, the data type is required as an object, this object can be used.
领英推荐
Wrapper classes include methods to unwrap the object and give back the primitive data type. This operation of converting objects to primitive data types is known as ?Unboxing.
What is Primitive Type in Java?
The primitive data types are the predefined data types provided by the Java programming language. There are eight primitive types. They are byte, short, int, long, float, double, boolean and char. The byte data type is used to store an 8-bit signed two’s complement integer. The short data type is used to store 16-bit signed two’s complement integer. An int data type is used to store 32-bit signed two’s complement integer while long data type is used to store 64-bit singed two’s complement integer. The float is used to store single precision 32-bit floating point value and the double is used to store double precision 64-bit floating point value. The boolean is used to represent true or false. The char is used to store a single character. Those are the eight primitive types in Java.
Wrapper?Class?vs Primitive Type in Java
Java language provides eight primitive data types. Sometimes it is required to convert the primitive types to object and also to convert the objects back to primitives. Wrapper classes can be used to achieve that task. The difference between wrapper class and primitive type in Java is that wrapper class is used to convert a primitive type to an object and object back to a primitive type while a primitive type is a predefined data type provided by the Java programming language.
What is the Similarity Between Wrapper Class and Primitive Type in Java?
Wrapper Class to Primitive Data Type
Wrapper classes in Java provide a way to convert between primitive data types and their corresponding objects. This conversion, known as unboxing, allows for seamless interchangeability and enables access to the primitive value within the wrapper class object.
Primitive Data Type to Wrapper Class
Java allows the conversion from primitive data types to their corresponding wrapper classes, known as autoboxing. This automatic conversion simplifies code by enabling direct assignment of primitive values to wrapper class objects, facilitating operations that require objects instead of primitives.
Boxing and Unboxing
Wrapper classes facilitate the process of converting between primitive types and objects through boxing and unboxing. Boxing involves wrapping a primitive value within its corresponding wrapper class object, while unboxing extracts the primitive value from the wrapper object. This allows you to work with primitive types in I/O operations that require objects.
Conclusion:
Java Wrapper classes offer several advantages over primitive data types, including encapsulation, type conversion, autoboxing and unboxing, and utility methods. They are also thread-safe and immutable, making them ideal for multi-threaded applications.
?? Java Enthusiast |Tech Talk Curator??|Join me for cutting-edge tech updates ??|Chairperson of IEEE SB NBKRIST| ISC2 Candidate| Undergraduate CSE student at N.B.K.R Institute of Science & Technology| Volunteer
7 个月Informative ?? autoboxing and unboxing is one of the important concepts in Java, Thanks for sharing ?