Challenge - 5 Problems
Primitive to Object Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of autoboxing with Integer
What is the output of this Java code snippet?
Java
Integer a = 1000; Integer b = 1000; System.out.println(a == b);
Attempts:
2 left
💡 Hint
Remember how Java caches Integer objects for certain values.
✗ Incorrect
Java caches Integer objects for values between -128 and 127. Since 1000 is outside this range, a and b refer to different objects, so '==' returns false.
❓ Predict Output
intermediateUnboxing and arithmetic with Integer and int
What is the output of this Java code?
Java
Integer x = 5; int y = 10; System.out.println(x + y);
Attempts:
2 left
💡 Hint
Think about how Java converts Integer to int automatically.
✗ Incorrect
Java automatically unboxes the Integer x to int, then adds it to y, resulting in 15.
🔧 Debug
advancedIdentify the error in primitive to object conversion
Which option will cause a compilation error in this code snippet?
Java
Double d = 10.5; Integer i = d;
Attempts:
2 left
💡 Hint
Check if direct assignment between wrapper classes is allowed.
✗ Incorrect
You cannot assign a Double object directly to an Integer reference. This causes a compilation error.
📝 Syntax
advancedCorrect way to convert int to Integer
Which option correctly converts a primitive int to an Integer object?
Attempts:
2 left
💡 Hint
Consider the recommended way to create Integer objects in modern Java.
✗ Incorrect
Integer.valueOf(5) is preferred because it uses caching and avoids unnecessary object creation.
🚀 Application
expertPredict the output with mixed primitive and wrapper types
What is the output of this Java code?
Java
Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println(a == b); System.out.println(c == d);
Attempts:
2 left
💡 Hint
Remember the Integer cache range in Java.
✗ Incorrect
Integer values between -128 and 127 are cached, so a and b refer to the same object (true). Values 128 are not cached, so c and d are different objects (false).
