The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive. Show
Since J2SE 5.0, autoboxing and unboxing feature convert primitives into objects and objects into primitives automatically. The automatic conversion of primitive into an object is known as autoboxing and vice-versa unboxing. Use of Wrapper classes in JavaJava is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.
The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes are given below: AutoboxingThe automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short. Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the primitive into objects. Wrapper class Example: Primitive to Wrapper Output: UnboxingThe automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives. Wrapper class Example: Wrapper to Primitive Output: Java Wrapper classes ExampleOutput: ---Printing object values--- Byte object: 10 Short object: 20 Integer object: 30 Long object: 40 Float object: 50.0 Double object: 60.0 Character object: a Boolean object: true ---Printing primitive values--- byte value: 10 short value: 20 int value: 30 long value: 40 float value: 50.0 double value: 60.0 char value: a boolean value: true Custom Wrapper class in JavaJava Wrapper classes wrap the primitive data types, that is why it is known as wrapper classes. We can also create a class which wraps a primitive data type. So, we can create a custom wrapper class in Java. Introduction to oops concept in Java: why the object-oriented program is useful in java? As we all know that java is an object-oriented programming language(oops) that is, java supports object-oriented concepts. As we all know that java is not purely object-oriented programming language(because it supports primitive data types) sometimes we have to include oops concept in java to fulfil our requirements by creating objects to the data created. We use object-oriented programming because it enables re-usability, easier to maintain software, define abstract data types. Introduction to wrapper classes in Java: before going into auto-boxing and unboxing let us understand what are wrapper classes. Wrapper classes: As the name itself suggests that wrap means combining and these are known as wrapper classes because they wrap or combine primitive data types into their corresponding object of wrapper classes. Hence, this is a brief explanation of wrapper classes in java. So we get a now clear idea about oops and wrapper classes so far. You might have a question that why are we discussing oops concept and wrapper classes. Don’t worry! Here you will get a detailed explanation about why we have discussed oops concept and wrapper classes in java. Introduction to auto-boxing and unboxing in Java:As we all know the data input in java is expected in two types of data format i.e., primitive data types and reference data types. To convert primitive data types to reference data type and vice-versa, we use the concept of auto-boxing and unboxing in java. As discussed earlier, wrapper classes in java added TWO important features:
Before knowing the above features in detail, let us learn about primitive data types and their corresponding wrapper classes. Primitive data types: byte, short, int, long, float, double, char, boolean. NOTE: From the above, we can say that the wrapper classes are same as primitive data types as they only differ from their naming convention i.e., all the wrapper classes must start with upper-case letters. You guys might get a question that why are we discussing primitive data types and their corresponding wrapper classes. Don’t worry! Here we will discuss in-detail about auto-boxing and unboxing. AUTO-BOXING: Auto-boxing is one of the features of wrapper classes. As the name itself suggests that auto-boxing helps in conversion of primitive data types into their corresponding wrapper classes automatically. simple example 1: int a = (you can enter any number of your own choice); example 2: int i = (you can enter any number of your own choice); NOTE: In the above, auto-boxing can be done in two ways i.e, using constructors and method(valueof()) UNBOXING: Unboxing is also one of the features of wrapper classes. This helps in the conversion of an object of the wrapper class into its primitive data type. It is exactly the opposite process of auto-boxing. a simple example: Integer obj1 = new Integer (enter number of your choice); int c = obj1.intvalue(); int a = obj1; // unboxing using methods NOTE: In the above; unboxing is only done by methods.
|