What is the method in Java to convert a string to an integer?

In Java, you can use the Integer.parseInt() method to convert a string to an integer. Here is an example:

String str = "123";
int num = Integer.parseInt(str);
System.out.println(num); // 输出 123

It is important to note that if the string is not in a valid integer format, such as containing letters or pure numbers exceeding the integer range, a NumberFormatException exception will be thrown. Therefore, when using the Integer.parseInt() method, make sure that the string is in a valid integer format.

bannerAds