Hi all,
I wanted to create this question/answer on how to convert a string to an integer in Java as this is something that I often get asked a lot about.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Introduction
Java was first released in 1995 and is one of the most popular programming languages right now. It is mainly used for the back-end side, for computing, and game development. Java is fast and secure, and it is also used almost everywhere.
The difference between a string and an int
There are 8 primitive data types in Java, but the ones that we are going to take a look at today are
String
andinteger
.Strings
are a sequence of characters. They can have up to 2,147,483,647 characters. They can be letters, numbers, or symbols. In order to set a string variable, we have to use the keywordString
, and after it, we set our variables name, and then we put in its value wrapped in quotations marks(" "
) or single quotation marks(''
) like this:Integers
are whole numbers. They can be any number from -2147483648 to 2147483647. Setting an integer is just like setting up a string, the only difference is that you need to use theint
keyword at the beginning, and the value that you are going to store in, doesn’t need to be wrapped in quotation marks. But what exactly is the difference if, for example, we had a string with a whole number for its value and an integer?Integers always hold in numeric values, but strings hold in just characters. If an integer sees its value as a number, the string sees its value as a character. This means that you can do math with integers, but not with strings.
So if you want to convert your string into an integer, there is a solution for you.
Let’s say you make a variable called
data
with the value"23"
in it:Next, let’s learn how we could convert a string into an integer in Java using
Integer.parseInt()
.Convert a string into an integer in Java using
Integer.parseInt()
Now luckily for us, there is a method called
Integer.parseInt()
, which makes our life much easier. Let’s say, that we want to convert ourdata
variable into an integer. we can just make a new variable with the namenumber
, and add the method in its value like this:And now our
number
has a value of23
.Next, let’s review another way of doing this.
Use the
Integer.valueOf()
method to return the string as an integer objectThere is also another way to do this. You set a variable with the
Integer
keyword and then pass into its value theInteger.valueOf()
method. This will return an integer object, which would be extracted from the specified string. Here is how this would look like:This would also return
23
. A benefit of usingInteger.valueOf()
is that you can pass a String as well as an integer as the parameter, whereasInteger.parseInt()
only accepts a String as the parameter.Another thing to keep in mind is that
Integer.valueOf()
returns an Integer object compared toInteger.parseInt()
which only returns a primitave int value.Conclusion
What I would recommend is that you try to do this a couple of times by yourself to see how it works. I hope that this post has helped you!
Best, Bobby