Java MCQ: Which of the following is not a valid method for converting a string to an integer in Java?
a) Integer.parseInt()
b) Integer.valueOf()
c) String.toInt()
d) None of the above
Answer:
c) String.toInt()
Explanation:
In Java, there are several ways to convert a string to an integer. The Integer.parseInt()
method is commonly used to parse a string and return an integer value. Similarly, Integer.valueOf()
converts a string to an Integer
object, which can then be unboxed to an int.
The method String.toInt()
does not exist in Java and is not a valid way to convert a string to an integer. This is why String.toInt()
would result in a compilation error if used.
Knowing the correct methods for type conversion is essential for handling data correctly in Java, especially when dealing with user input or data parsing.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html