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.
Click below to sign up and get $200 of credit to try our products over 60 days!
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
Stringsare 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 keyword
String, 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:
Integersare 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 the
intkeyword 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
datawith the value
Next, let’s learn how we could convert a string into an integer in Java using
Convert a string into an integer in Java using
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 our
datavariable into an integer. we can just make a new variable with the name
number, and add the method in its value like this:
And now our
numberhas a value of
Next, let’s review another way of doing this.
Integer.valueOf()method to return the string as an integer object
There is also another way to do this. You set a variable with the
Integerkeyword and then pass into its value the
Integer.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 using
Integer.valueOf()is that you can pass a String as well as an integer as the parameter, whereas
Integer.parseInt()only accepts a String as the parameter.
Another thing to keep in mind is that
Integer.valueOf()returns an Integer object compared to
Integer.parseInt()which only returns a primitave int value.
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!