Sometimes, you need to use simple values like numbers or letters as objects. This helps when you want to use them in places that only accept objects, like collections.
Primitive to object conversion in Java
public class WrapperExample { public static void main(String[] args) { int primitiveInt = 5; Integer objectInt = Integer.valueOf(primitiveInt); // Convert int to Integer object char primitiveChar = 'a'; Character objectChar = Character.valueOf(primitiveChar); // Convert char to Character object } }
Java provides wrapper classes like Integer, Character, Double, etc. to convert primitives to objects.
You can also use autoboxing, where Java automatically converts primitives to objects.
int primitiveInt = 10; Integer objectInt = Integer.valueOf(primitiveInt);
int primitiveInt = 20; Integer objectInt = primitiveInt; // Autoboxing
char primitiveChar = 'z'; Character objectChar = Character.valueOf(primitiveChar);
char primitiveChar = 'x'; Character objectChar = primitiveChar; // Autoboxing
This program shows how to convert primitive int and char values to their object wrappers using both explicit conversion and autoboxing. It prints all values before and after conversion.
public class PrimitiveToObjectConversion { public static void main(String[] args) { // Primitive int int primitiveNumber = 42; System.out.println("Primitive int: " + primitiveNumber); // Convert primitive int to Integer object explicitly Integer objectNumber = Integer.valueOf(primitiveNumber); System.out.println("Integer object (explicit): " + objectNumber); // Autoboxing: automatic conversion Integer autoBoxedNumber = primitiveNumber; System.out.println("Integer object (autoboxing): " + autoBoxedNumber); // Primitive char char primitiveLetter = 'J'; System.out.println("Primitive char: " + primitiveLetter); // Convert primitive char to Character object explicitly Character objectLetter = Character.valueOf(primitiveLetter); System.out.println("Character object (explicit): " + objectLetter); // Autoboxing for char Character autoBoxedLetter = primitiveLetter; System.out.println("Character object (autoboxing): " + autoBoxedLetter); } }
Time complexity: Conversion is very fast, essentially constant time.
Space complexity: Wrapper objects use more memory than primitives because they store extra information.
Common mistake: Confusing primitive comparison (==) with object comparison (.equals()). Use .equals() to compare wrapper objects.
Use primitive to object conversion when you need to store primitives in collections like ArrayList, which only accept objects.
Primitive to object conversion lets you use simple values as objects.
Java provides wrapper classes like Integer and Character for this purpose.
Autoboxing makes conversion automatic and easy.
Practice
What is autoboxing in Java?
Solution
Step 1: Understand primitive and wrapper classes
Java has primitive types like int and wrapper classes like Integer that represent them as objects.Step 2: Define autoboxing
Autoboxing is the automatic conversion from a primitive type to its corresponding wrapper class object by the compiler.Final Answer:
Automatic conversion of primitive types to their wrapper class objects -> Option DQuick Check:
Autoboxing = automatic primitive to object conversion [OK]
- Confusing autoboxing with unboxing
- Thinking autoboxing requires manual code
- Mixing up primitive to object with object to primitive
Which of the following is the correct way to create an Integer object from a primitive int value 5 using autoboxing?
Solution
Step 1: Recall autoboxing syntax
Autoboxing allows assigning a primitive int directly to an Integer object without explicit conversion.Step 2: Analyze options
Direct assignment Integer num = 5; triggers autoboxing. new Integer(5); uses deprecated constructor (manual). Integer.valueOf("5"); converts from String, not primitive. (Integer)5; is invalid cast as primitives cannot be cast to wrapper objects.Final Answer:
Integer num = 5; -> Option AQuick Check:
Autoboxing allows direct assignment int to Integer [OK]
- Using deprecated Integer constructor
- Trying to cast primitive to wrapper
- Confusing String conversion with autoboxing
What will be the output of the following Java code?
int x = 10; Integer y = x; System.out.println(y + 5);
Solution
Step 1: Understand autoboxing and unboxing in the code
Variable x is primitive int 10. Assigning to Integer y autoboxes int to Integer object. In print, y is unboxed to int for addition.Step 2: Calculate the expression
y + 5 becomes 10 + 5 = 15, so output is 15.Final Answer:
15 -> Option AQuick Check:
Autoboxing and unboxing allow arithmetic: 10 + 5 = 15 [OK]
- Thinking y + 5 concatenates as string
- Expecting compilation error due to object usage
- Confusing autoboxing with no conversion
Identify the error in the following code snippet:
int a = 7; Integer b = (Integer) a; System.out.println(b);
Solution
Step 1: Analyze the cast operation
The code tries to cast primitive int 'a' to Integer using (Integer) a, which is invalid in Java.Step 2: Correct way to convert int to Integer
Autoboxing allows direct assignment without cast: Integer b = a; Casting primitive to wrapper is not allowed.Final Answer:
Cannot cast primitive int to Integer directly -> Option BQuick Check:
Primitive to wrapper cast is invalid, use autoboxing [OK]
- Trying to cast primitive to wrapper type
- Ignoring autoboxing feature
- Assuming Integer can't hold int values
Given the following code, what will be the output?
int[] nums = {1, 2, 3};
Integer[] boxedNums = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
boxedNums[i] = nums[i];
}
System.out.println(java.util.Arrays.toString(boxedNums));Solution
Step 1: Understand array and autoboxing in loop
nums is int array. boxedNums is Integer array. Loop assigns each int to Integer element, autoboxing each value.Step 2: Print the Integer array
Arrays.toString prints the Integer array elements as their values: [1, 2, 3]. No nulls because all elements assigned.Final Answer:
[1, 2, 3] -> Option CQuick Check:
Autoboxing fills Integer array from int array correctly [OK]
- Expecting nulls because of object array
- Thinking array assignment causes error
- Confusing array length or extra elements
Ready to prove it?
Pass the test and this pattern moves unknown
