Convert String to int in Java

Converting a String to an int or Integer is a common task in Java, and there are several approaches to accomplish this. One of the most commonly used methods is Integer.parseInt(), which allows you to parse a String and obtain the corresponding integer value.

Integer.parseInt() method

The Integer.parseInt() method takes a String as input and returns the parsed integer value. It performs the necessary conversion by interpreting the characters in the String as digits and converting them to their numeric representation. If the String contains non-numeric characters or is not a valid representation of an integer, a NumberFormatException will be thrown.

To convert a String to an int using Integer.parseInt(), you can simply pass the String as an argument to the method, like this:

String strNum="999"; int numStr = Integer.parseInt(strNum);

The value of numStr would be 999.

If the first character is a minus(-) sign it will return a minus value.

String strNum="-999"; int numStr = Integer.parseInt(strNum);

The value of numStr would be -999.

If the given String is not valid, conversion will throws a NumberFormatException .

String strNum="asdf"; int numStr = Integer.parseInt(strNum);

It is important to note that when using Integer.parseInt(), the String must contain a valid numeric representation of an integer. If the String is empty, null, or contains non-numeric characters, a NumberFormatException will be thrown. Therefore, it is recommended to handle exceptions appropriately when using this method.

In addition to Integer.parseInt(), there are other methods available in the Integer class, such as Integer.valueOf(), that provide different ways to convert a String to an Integer object or primitive int value. The choice of method depends on your specific requirements and the error handling approach you want to adopt.

Example
class TestClass{ public static void main (String[] args){ String strNum="999"; int numStr = Integer.parseInt(strNum); System.out.println("Output is " + numStr); String strNum1="-999"; int numStr1 = Integer.parseInt(strNum1); System.out.println("Output is " + numStr1); } }
Output
Value is 999 Value is -999

Integer.valueOf()

Java's Integer.valueOf() method is used to convert an int value to an Integer object. It returns an Integer instance that represents the specified int value.

The Integer.valueOf() method takes an int value as its parameter and returns an Integer object that wraps around the specified value. This allows you to work with the int value as an object and access various utility methods provided by the Integer class.

Here's an example of using Integer.valueOf() to convert an int value to an Integer object:

class TestClass{ public static void main (String[] args){ String strNum="999"; Integer numStr = Integer.valueOf(strNum); System.out.println("Value is " + numStr); } }
Output
Value is 999

Integer.valueOf() returns a reference to a cached Integer object for values in the range -128 to 127. For values outside this range, a new Integer object is created. This caching mechanism helps improve performance and memory efficiency when working with frequently used integer values.

Integer's Constructor

You can convert a string to an integer value using the Integer class's constructor. The Integer class provides a constructor that accepts a string as an argument and returns an Integer object representing the parsed integer value.

Here's an example of using the Integer constructor to convert a string to an integer:

class TestClass{ public static void main (String[] args){ String strNum="999"; Integer numStr = new Integer(strNum); System.out.println("Value is " + numStr); } }
Output
Value is 999

In the above code, the string "999" is passed as an argument to the Integer constructor, which creates an Integer object representing the integer value 999. This Integer object is then automatically unboxed to an int and assigned to the 'number' variable.

In some cases the Integer constructor can throw a NumberFormatException if the string does not represent a valid integer. Therefore, it's a good practice to handle this exception or use appropriate error handling mechanisms when performing the conversion.

Difference between parseInt() and valueOf()

The main difference between Integer.valueOf() and Integer.parseInt() lies in the return types of these methods.

Integer.valueOf()

  1. Returns an Integer object that represents the parsed integer value.
  2. The return type is Integer, which is an object wrapper class for the int primitive type.
  3. This method is useful when you need to perform operations that require an Integer object, such as storing the result in a collection or passing it as an argument to a method that expects an Integer.

Integer.parseInt()

  1. Returns an int primitive value that represents the parsed integer.
  2. The return type is int, which is a primitive data type.
  3. This method is commonly used when you need to perform arithmetic operations or assign the integer value to a variable of primitive type.

Conclusion

You can convert a string to an int using the Integer.parseInt() method. This method takes a string as input and returns the corresponding integer value. If the string contains non-numeric characters or is not a valid integer representation, it will throw a NumberFormatException, so it's essential to handle this exception when using the parseInt() method.