In Java, you can use the Integer.parseInt() method to convert a string representation of an integer value into an int primitive type.
Here's an example of how you can use the Integer.parseInt() method to convert a string to an int value:
String str = "123"; int i = Integer.parseInt(str);
In this example, the str variable is a string that represents the integer value 123. The Integer.parseInt() method is called on the str variable and the resulting int value is stored in the i variable.
The Integer.parseInt() method takes a string as an argument and returns an int value. If the string is not a valid integer representation, the method will throw a NumberFormatException.
You can also use the Integer.valueOf() method to convert a string to an Integer object, which is the wrapper class for the int primitive type. The Integer.valueOf() method works in a similar way to the Integer.parseInt() method, but it returns an Integer object instead of an int value.
Here's an example of how you can use the Integer.valueOf() method to convert a string to an Integer object:
String str = "123"; Integer i = Integer.valueOf(str);
In this example, the str variable is a string that represents the integer value 123. The Integer.valueOf() method is called on the str variable and the resulting Integer object is stored in the i variable.
It's important to note that both the Integer.parseInt() and Integer.valueOf() methods can throw a NumberFormatException if the string is not a valid integer representation. You should catch this exception or use try-with-resources to handle it appropriately.
You can also use the Long.parseLong(), Double.parseDouble(), and Float.parseFloat() methods to convert a string to a long, double, and float value, respectively. These methods work in a similar way to the Integer.parseInt() method, but they return a long, double, or float value instead of an int value. Similarly, you can use the Long.valueOf(), Double.valueOf(), and Float.valueOf() methods to convert a string to a Long, Double, and Float object, respectively.