Java Five programs to wrapper classes. The wrapper concept in Java allows primitive data types to be wrapped into objects, enabling them to be treated like objects.Java offers a set of wrapper classes corresponding to each primitive data type.. Here’s an overview along with five simple programs to illustrate the wrapper concept step by step.
Overview of Wrapper Classes
Java provides wrapper classes for the following primitive types:
int → Integer
char → Character
double → Double
boolean → Boolean
byte → Byte
short → Short
long → Long
float → Float
These classes allow you to convert primitive types into objects and vice versa.
1: Basic Wrapping
Objective: Create an Integer object from an int primitive.
java public class WrapperExample1 { public static void main(String[] args) { int primitiveInt = 10; // Wrapping the primitive int into an Integer object Integer wrappedInt = Integer.valueOf(primitiveInt); // Displaying values System.out.println("Primitive int: " + primitiveInt); System.out.println("Wrapped Integer: " + wrappedInt); } }
Explanation:
1. An `int` primitive is defined.
2. The primitive is wrapped using `Integer.valueOf()`.
3. Both the primitive and wrapped values are printed.
2: Unwrapping
Objective: Convert an Integer object back to an int.
java public class WrapperExample2 { public static void main(String[] args) { Integer wrappedInt = 20; // Auto-boxing // Unwrapping the Integer object to primitive int int primitiveInt = wrappedInt.intValue(); // Displaying values System.out.println("Wrapped Integer: " + wrappedInt); System.out.println("Unwrapped int: " + primitiveInt); } }
Explanation:
1. An `Integer` object is created.
2. The object is unwrapped using `intValue()`.
3. Both the wrapped and unwrapped values are printed.
3: Autoboxing and Unboxing
Objective: Demonstrate automatic conversion between primitive types and their corresponding wrapper classes.
java public class WrapperExample3 { public static void main(String[] args) { int primitiveInt = 30; // Autoboxing: the automatic transformation of a primitive type into its corresponding wrapper class. Integer wrappedInt = primitiveInt; // Unboxing: the automatic conversion of a wrapper class back into its corresponding primitive type. int unwrappedInt = wrappedInt; // Displaying values System.out.println("Primitive int: " + primitiveInt); System.out.println("Wrapped Integer: " + wrappedInt); System.out.println("Unwrapped int: " + unwrappedInt); } }
Explanation:
1. An `int` primitive is defined.
2. The primitive is automatically wrapped into an `Integer` (autoboxing).
3. The wrapped `Integer` is automatically unwrapped back to `int` (unboxing).
4. All values are printed.
4: Wrapper Class Methods
Objective: Use methods provided by wrapper classes. (Java Five programs to generate wrapper concept in java.)
java public class WrapperExample4 { public static void main(String[] args) { String numberString = "100"; // Parsing string to Integer Integer wrappedInt = Integer.parseInt(numberString); // Displaying values System.out.println("String: " + numberString); System.out.println("Parsed Integer: " + wrappedInt); // Converting Integer back to String String strFromInteger = wrappedInt.toString(); System.out.println("Converted back to String: " + strFromInteger); } }
Explanation:
1. A string representing a number is defined.
2. The string is parsed into an `Integer` using `Integer.parseInt()`.
3. The `Integer` is converted back to a string using `toString()`.
4. All values are printed.
5: Collections with Wrapper Classes
Objective: Use wrapper classes with Java Collections.
java import java.util.ArrayList; public class WrapperExample5 { public static void main(String[] args) { ArrayList<Integer> intList = new ArrayList<>(); // Adding primitive ints (autoboxing) intList.add(10); intList.add(20); intList.add(30); // Displaying values from ArrayList for (Integer num : intList) { System.out.println("Number: " + num); } } }
Explanation:
1. An `ArrayList` of `Integer` is created.
2. Primitive `int` values are added to the list (autoboxing occurs).
3. The values from the list are printed using a loop.
Conclusion
These programs demonstrate the wrapper concept in Java, highlighting how to create wrapper objects, convert between primitives and objects, utilize wrapper methods, and work with collections. The use of wrappers is fundamental in Java, especially when working with collections and generics.