Wrapper classes in Java provide a way to use primitive data types (e.g., int, double) as objects. These classes are part of the java.lang package and are essential for various Java features like generics, collections, and frameworks that require objects instead of primitives.
List of Wrapper Classes:
| Primitive Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Features of Wrapper Classes:
- Boxing:
- Converting a primitive type into its corresponding wrapper class object.
- Example:
int num = 5; Integer boxedNum = Integer.valueOf(num); // Explicit boxing Integer autoBoxedNum = num; // Auto-boxing
- Unboxing:
- Converting a wrapper class object back to its corresponding primitive type.
- Example:
Integer obj = Integer.valueOf(10); int unboxedNum = obj.intValue(); // Explicit unboxing int autoUnboxedNum = obj; // Auto-unboxing
Example: Using Wrapper Classes in Java
import java.util.ArrayList;
import java.util.List;
public class WrapperExample {
public static void main(String[] args) {
// Auto-boxing
List<Integer> numbers = new ArrayList<>();
numbers.add(10); // Auto-boxes primitive int to Integer
numbers.add(20);
// Auto-unboxing
int sum = 0;
for (Integer num : numbers) {
sum += num; // Auto-unboxes Integer to int
}
System.out.println("Sum: " + sum);
}
}
All Possible Combinations of Wrapper Classes:
- Boxing and Unboxing with All Primitive Types:
public class WrapperDemo {
public static void main(String[] args) {
// byte
byte b = 1;
Byte byteObj = b; // Auto-boxing
byte bUnboxed = byteObj; // Auto-unboxing
// short
short s = 2;
Short shortObj = s;
short sUnboxed = shortObj;
// int
int i = 3;
Integer intObj = i;
int iUnboxed = intObj;
// long
long l = 4L;
Long longObj = l;
long lUnboxed = longObj;
// float
float f = 5.5f;
Float floatObj = f;
float fUnboxed = floatObj;
// double
double d = 6.6;
Double doubleObj = d;
double dUnboxed = doubleObj;
// char
char c = 'A';
Character charObj = c;
char cUnboxed = charObj;
// boolean
boolean bool = true;
Boolean boolObj = bool;
boolean boolUnboxed = boolObj;
// Print results
System.out.println("Byte: " + bUnboxed);
System.out.println("Short: " + sUnboxed);
System.out.println("Int: " + iUnboxed);
System.out.println("Long: " + lUnboxed);
System.out.println("Float: " + fUnboxed);
System.out.println("Double: " + dUnboxed);
System.out.println("Char: " + cUnboxed);
System.out.println("Boolean: " + boolUnboxed);
}
}
- Comparing Wrapper Objects:
public class WrapperComparison {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true (cached within -128 to 127)
Integer c = 128;
Integer d = 128;
System.out.println(c == d); // false (outside cache range)
System.out.println(a.equals(b)); // true
System.out.println(c.equals(d)); // true
}
}
Converting Between int and String:
- Converting
inttoString:
public class IntToStringDemo {
public static void main(String[] args) {
int number = 123;
// Using String.valueOf()
String str1 = String.valueOf(number);
// Using Integer.toString()
String str2 = Integer.toString(number);
// Print results
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
}
}
- Converting
Stringtoint:
public class StringToIntDemo {
public static void main(String[] args) {
String str = "456";
// Using Integer.parseInt()
int num1 = Integer.parseInt(str);
// Using Integer.valueOf()
int num2 = Integer.valueOf(str);
// Print results
System.out.println("Number 1: " + num1);
System.out.println("Number 2: " + num2);
}
}
When to Use Wrapper Classes:
- Collections: Primitive types cannot be stored in collections like
ArrayList,HashMap, etc. - Reflection: Wrapper classes are useful when working with Java Reflection.
- Serialization: Objects can be serialized, but primitive types cannot.
- Generics: Java generics work only with objects, not primitives.
Wrapper classes bridge the gap between primitive types and object-oriented features in Java, enabling a wide range of functionalities and flexibility.