Question

How to convert a string to an integer in Java?

Posted September 24, 2021 97 views
JavaProgramming ProjectVS Code

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.

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.

×
Submit an Answer
1 answer

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 and integer.

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 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:

String name = "Bruce";

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 the int 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:

String data = "23";

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 our data variable into an integer. we can just make a new variable with the name number, and add the method in its value like this:

int number = Integer.parseInt(data);

And now our number has a value of 23.

Next, let’s review another way of doing this.

Use the 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 Integer keyword 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:

Integer number = Integer.valueOf(data);

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.

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