How To Convert a String to Int in Java

QASource Engineering Team | March 31, 2025

How To Convert a String to Int in Java

In Java, converting a String to an int is a common requirement, especially when working with user input, file parsing, or data received from external sources like APIs. Fortunately, Java provides multiple built-in methods to perform this conversion efficiently and safely.

Below are the most commonly used techniques:

  1. Using Integer.parseInt() (Most Common Method)

    This is the recommended and most efficient way to convert a String to an int.

    public class Main {
    	public static void main(String[] args) {
        	String numberString = "123";
        	int number = Integer.parseInt(numberString);
        	System.out.println("Converted int: " + number);
    	}
    }
    

    Pros: Simple, fast, and returns a primitive int.

    Note: Throws NumberFormatException if the string is not a valid integer.

  2. Using Integer.valueOf() (Returns Integer Object)

    This method converts the String to an Integer (object), which can be unboxed to int.

    public class Main {
    	public static void main(String[] args) {
        	String numberString = "456";
        	int number = Integer.valueOf(numberString);
        	System.out.println("Converted int: " + number);
    	}
    }
    

    Pros: Useful when you specifically need an Integer object.

    Note: Also throws NumberFormatException for invalid input.

  3. Using parseInt() with a Specific Radix

    You can also specify a radix (base) while parsing the string. This is especially helpful when dealing with binary, hexadecimal, or octal numbers.

    public class Main {
    	public static void main(String[] args) {
        	String binaryString = "1010";
        	int number = Integer.parseInt(binaryString, 2); // Binary to Decimal
        	System.out.println("Binary to int: " + number);
    	}
    }
    

    Pros: Converts numbers in various numeral systems to int.

    Note: Parsing binary (2), octal (8), decimal (10), or hexadecimal (16) strings.

 

Exception Handling Tip

All the above methods can throw a NumberFormatException if the input string is not a valid number. Wrapping the parsing code in a try-catch block is a good practice to handle such scenarios gracefully.

try {
    int number = Integer.parseInt("abc"); // Will throw exception
} catch (NumberFormatException e) {
    System.out.println("Invalid number format!");
}
 

Final Thoughts

Choosing the right method depends on your use case. If you just need a primitive int, go with Integer.parseInt(). If you require an Integer object (e.g., for collections like List<Integer>), use Integer.valueOf(). When dealing with numbers in different bases, the radix-aware version of parseInt() is your go-to tool.

Always remember to validate or handle exceptions when working with external or user-provided data to avoid runtime errors.

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.

Post a Comment

Categories