Tuesday, February 5, 2019

How do I convert a String to an int in Java?

For example, here are two ways:
Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);
There is a slight difference between these methods:
  • valueOf returns a new or cached instance of java.lang.Integer
  • parseInt returns primitive int.

No comments:

Post a Comment