How to convert String to int or Integer data type in Java? Example Tutorial (original) (raw)

Hello guys, if you are wondering how to convert a String variable to int or Integer variable in Java then you have come to the right place. Earlier, I have showed how to convert Integer to String in Java and today, you will learn about how to convert String to int or Integer variable in Java. There are 3 main ways to convert String to int in Java, first, by using the constructor of Integer class, second, by using parseInt() method of java.lang.Integer, and third, by using Integer.valueOf() method. Though all those methods return an instance of java.lang.Integer, which is a wrapper class for primitive int value, it's easy to convert Integer to int in Java.

From Java 5, you don't need to do anything, autoboxing will automatically convert Integer to int. For Java 1.4 or lower version, you can use the intValue() method from java.lang.Integer class, to convert Integer to int.

As the name suggests, parseInt() is the core method to convert String to int in Java. parseInt() accept a String which must contain decimal digits and the first character can be an ASCII minus sign (-) to denote negative integers. parseInt() throws NumberFormatException, if provided String is not convertible to int value.

By the way, parseInt is an overloaded method and its overloaded version takes radix or base e.g. 2,8,10 or 16, which can be used to convert binary, octal, hexadecimal String to int in Java. Integer.valueOf() is another useful method to convert String to Integer in Java, it offers caching of Integers from -128 to 127.

Internally, valueOf() also calls parseInt() method for String to int conversion. In this Java programming tutorial, we will see all three ways to convert String to an int value in Java.

String to int and Integer Conversion in Java Example

This Java program converts both positive and negative numeric String to int in Java. As I said, it uses three methods. The first example uses the constructor of the Integer class to create an Integer from String, later the Integer is converted to int using autoboxing in Java.

Second example uses Integer.parseInt() and third String conversion examples use Integer.valueOf() method to convert String to int in Java.

How to convert String to int or Integer in Java? Example Tutorial

By the way, be careful with String.valueOf() method, as it may return the same object on multiple classes if the integer value is from its cache. Also comparing int to Integer is error-prone in autoboxing and can produce surprising results. See pitfalls of autoboxing in Java for more details.

package test;

/**

}

Output: Integer created from String in Java : 123 String to int using parseInt() : 123 String to int Conversion example using valueOf : 123 Converting negative String to int in Java String to Integer Constructor Example : -123 String to Integer parseInt Example : -123 String Integer valueOf Example : -123

That's all on How to convert String to int or Integer in Java. By the way, converting int to Integer is ridiculously easy in Java and done automatically by the Java platform, from Java 5 onwards.

3 ways to convert String to Integer in Java with ExampleAs said in the best way to convert String to number in Java, you can choose between parseInt() and valueOf() for String to int conversion. Integer.valueOf() is preferred because it offers caching and eventually delegates the call to parseInt for String to int conversion if an integer value is not available in the cache.

Related Java Programming Tutorials for Java programmers