Bird
0
0

What happens when you assign an int value directly to an Integer variable in Java?

easy📝 Conceptual Q1 of 15
Java - Wrapper Classes
What happens when you assign an int value directly to an Integer variable in Java?
AA compile-time error occurs because types are incompatible.
BThe program throws a runtime exception.
CThe <code>Integer</code> variable stores the primitive <code>int</code> value directly.
DThe <code>int</code> is automatically converted to an <code>Integer</code> object using autoboxing.
Step-by-Step Solution
Solution:
  1. Step 1: Understand autoboxing concept

    Autoboxing automatically converts primitive types like int to their wrapper classes like Integer.
  2. Step 2: Assigning int to Integer

    When you assign an int to an Integer, Java wraps the primitive into an object automatically.
  3. Final Answer:

    The int is automatically converted to an Integer object using autoboxing. -> Option D
  4. Quick Check:

    Autoboxing = automatic primitive to wrapper conversion [OK]
Quick Trick: Autoboxing wraps primitives automatically when assigned to wrapper types [OK]
Common Mistakes:
  • Thinking autoboxing requires explicit constructor calls
  • Confusing autoboxing with casting
  • Assuming primitives can be stored directly in wrapper variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes