9

String to Int in Java – How to Convert a String to an Integer

 11 months ago
source link: https://www.freecodecamp.org/news/how-to-convert-a-string-to-an-integer-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

November 3, 2022 / #Java

String to Int in Java – How to Convert a String to an Integer

String to Int in Java – How to Convert a String to an Integer

When working with a programming language, you may want to convert strings to integers. An example would be performing a mathematical operation using the value of a string variable.

In this article, you'll learn how to convert a string to an integer in Java using two methods of the Integer class — parseInt() and valueOf().

How to Convert a String to an Integer in Java Using Integer.parseInt

The parseInt() method takes the string to be converted to an integer as a parameter. That is:

Integer.parseInt(string_varaible)

Before looking at an example of its usage, let's see what happens when you add a string value and an integer without any sort of conversion:

class StrToInt {
    public static void main(String[] args) {
        String age = "10";
        
        System.out.println(age + 20);
        // 1020
    }
}

In the code above, we created an age variable with a string value of "10".

When added to an integer value of 20, we got 1020 instead of 30.

Here's a quick fix using the parseInt() method:

class StrToInt {
    public static void main(String[] args) {
        String age = "10";
        
        int age_to_int = Integer.parseInt(age);
        
        System.out.println(age_to_int + 20);
        // 30
    }
}

In order to convert the age variable to an integer, we passed it as a parameter to the parseInt() method — Integer.parseInt(age) — and stored it in a variable called age_to_int.

When added to another integer, we got a proper addition: age_to_int + 20.

How to Convert a String to an Integer in Java Using Integer.valueOf

The valueOf() methods works just like the parseInt() method. It takes the string to be converted to an integer as its parameter.

Here's an example:

class StrToInt {
    public static void main(String[] args) {
        String age = "10";
        
        int age_to_int = Integer.valueOf(age);
        
        System.out.println(age_to_int + 20);
        // 30
    }
}

The explanation for the code above is the same as the last section:

  • We passed the string as a parameter to valueOf(): Integer.valueOf(age). It was stored in a variable called age_to_int.
  • We then added 20 to the variable created: age_to_int + 20. The resulting value was 30 instead of 1020.

Summary

In this article, we talked about converting strings to integers in Java.

We saw how to convert a string to an integer in Java using two methods of the Integer class — parseInt() and valueOf().

Happy coding!



If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTISEMENT

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK