Bird
0
0

Which of the following methods converts a String to a primitive int in Java?

easy📝 Conceptual Q11 of 15
Java - Wrapper Classes
Which of the following methods converts a String to a primitive int in Java?
A<code>Integer.valueOf(String s)</code>
B<code>Integer.toString(int i)</code>
C<code>Integer.parseInt(String s)</code>
D<code>Integer.intValue()</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand method purpose

    parseInt converts a string directly to a primitive int.
  2. Step 2: Differentiate from other methods

    valueOf returns an Integer object, not a primitive. toString converts int to string. intValue() is called on an Integer object to get primitive int.
  3. Final Answer:

    Integer.parseInt(String s) -> Option C
  4. Quick Check:

    parseInt returns int [OK]
Quick Trick: parseInt returns primitive int, valueOf returns Integer object [OK]
Common Mistakes:
  • Confusing parseInt and valueOf
  • Using intValue() without an Integer object
  • Thinking toString converts string to int

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes