forked from hotwax/training-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2: Wrapper Class
Rishabh Malviya edited this page Feb 7, 2023
·
1 revision
- Wrapper classes are used to convert primitive data types into objects.
- The primitive data types are not objects. They are just data types. So, we need wrapper classes to convert primitive data types into objects.
- There are eight wrapper classes available in Java.
ByteShortIntegerLongFloatDoubleCharacterBoolean
- The wrapper classes are present in the
java.langpackage. - The wrapper classes are immutable.
-
AutoBoxingandUnboxingare the two features of wrapper classes.-
AutoBoxingis the automatic conversion of primitive data types into objects. -
Unboxingis the automatic conversion of objects into primitive data types.
-
import java.lang.*;
class Main{
public static void main(String[] args){
int a = 10;
Integer i = Integer.valueOf(a); // converting int into Integer
Integer j = a; // autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a + " " + i + " " + j);
}
// using autoboxing and auto unboxing
public static void main(String[] args){
Integer a = 10;
a = a + 10; // unboxing, now compiler will write a.intValue() internally
System.out.println(a);
int b = 20;
Integer c = b; // autoboxing, now compiler will write Integer.valueOf(b) internally
System.out.println(c);
}
}- There are different constructors available in the wrapper classes to create wrapper objects.
Integer i = new Integer(10);
Integer j = new Integer("10");- There are different methods available in the wrapper classes to perform operations on wrapper objects.
i.intValue(); // returns the value of this Integer as an int
i.byteValue(); // returns the value of this Integer as a byte
i.shortValue(); // returns the value of this Integer as a short
i.longValue(); // returns the value of this Integer as a longSimilarly, there are different methods available in the wrapper classes to perform operations on wrapper objects.
- Autoboxing is the automatic conversion of primitive data types into objects.
- The Java compiler applies autoboxing when a primitive value is:
- Passed as a parameter to a method that expects an object of the corresponding wrapper class.
- Assigned to a variable of the corresponding wrapper class.
import java.lang.Integer;
class Main{
public static void main(String[] args){
int a = 20;
Integer i = Integer.valueOf(a); // converting int into Integer
Integer j = a; // autoboxing, now compiler will write Integer.valueOf(a) internally
System.out.println(a + " " + i + " " + j);
}
}- Wrapper class object can be converted into primitive data types using the
intValue(),byteValue(),shortValue(),longValue(),floatValue(),doubleValue(),charValue()andbooleanValue()methods. -
intValue(),byteValue(),shortValue(),longValue(),floatValue(),doubleValue(),charValue()methods can be used to convert wrapper objects into primitive data types.
import jdk.incubator.foreign.SymbolLookup;
import java.lang.Integer;
class Main {
public static void main(String[] args) {
// Converting primitive data types into wrapper objects
Integer obj1 = Integer.valueOf(234)
Double obj2 = Double.valueOf(5.62)
Boolean obj3 = Boolean.valueOf(true);
// Converting wrapper objects into primitive data types
int var1 = obj1.intValue();
double var2 = obj2.doubleValue();
boolean var3 = obj3.booleanValue();
}
}