Bird
0
0

Which of the following is the correct syntax to convert an int primitive to an Integer object?

easy📝 Syntax Q3 of 15
Java - Wrapper Classes
Which of the following is the correct syntax to convert an int primitive to an Integer object?
AInteger obj = Integer.parseInt(intValue);
BInteger obj = Integer.valueOf(intValue);
CInteger obj = new Integer.parseInt(intValue);
DInteger obj = intValue.toInteger();
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct method to convert int to Integer object

    Integer.valueOf(intValue) returns an Integer object from int primitive.
  2. Step 2: Check syntax correctness of options

    Integer.parseInt returns int primitive, not Integer object. new Integer.parseInt is invalid syntax. intValue.toInteger() is invalid because int is primitive.
  3. Final Answer:

    Integer obj = Integer.valueOf(intValue); -> Option B
  4. Quick Check:

    int to Integer object uses valueOf [OK]
Quick Trick: Use Integer.valueOf to box int to Integer object [OK]
Common Mistakes:
  • Using parseInt which returns primitive int
  • Incorrect new keyword usage with parseInt
  • Calling methods on primitive int

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes