How do convert string to Integer or Integer to String in java?

As a developer, in day-to-day programming, we have encountered many situations to convert from String to Integer or vice versa.

The string object holds a sequence of characters enclosed in double quotes.

Integer object stores the numeric values.

The string should hold a valid int value. if an invalid numeric value exists in the string, then NumberFormatException is thrown. For this, you have to handle using try-and-catch blocks.

How to Convert String to Integer in Java

There are several ways to convert from String to integer. For this,

How to convert String to Integer using java Constructor

It is a simple and easy way to convert to Integer. First, Create an Integer object with the constructor passing String type.

String s="123";
Integer i=new Integer(s);

This conversion happens at object creation time.

How to convert String to Integer using Integer.parseInt() method

The Integer class has a parseInt method. We have used the Integer.parseInt() method to convert String to Integer.

It returns the primitive int value.

Syntax:

public static int parseInt(String s) throws NumberFormatException

Arguments: Numerical value in the form of string Throws: It throws NumberFormatException if the non-numerical string is passed.

public class Test {
    public static void main(String[] args) {
       String s="123";
       Integer i=Integer.parseInt(s);
    }
}

if an string contains non numerical value(abc), It thrown an error Exception in thread “main” java.lang.NumberFormatException.

public class Test {
    public static void main(String[] args) {
        int result;
        String s="abc";

        try {
            result = Integer.parseInt(s);
        }
        catch (NumberFormatException e){
            result = 0;
        }
    }
}

How to convert String to Integer using valueOf() method

another way is using valueOf of the Integer object

This returns thejava.lang.Integer object

Here is a syntax

public static Integer valueOf(String s) throws NumberFormatException

Arguments: Numerical value in the form of string Throws: It throws NumberFormatException if the non-numerical string is passed.

public class Test {
    public static void main(String[] args) {
        String s="123";
        Integer i=Integer.valueOf(s);
    }
}

Here is an example of a non-numerical string value

public class Test {
    public static void main(String[] args) {
        String s = "abc";
        Integer result;
        try {
             result= Integer.valueOf(s);
        } catch (NumberFormatException e) {
            System.out.println("Invalid integer value in String " + s
                    + "exception=" + e);
            result=0;
        }
    }
}

How to convert String to Integer using NumberUtils from apache commons library

if your project is using the apache commons library, org.apache.commons.lang3.math.NumberUtils class provides numeric utility functions.

It Has a toInt method which returns an integer if the input is a valid numeric value, else returns 0.

import org.apache.commons.lang3.math.NumberUtils;
public class App {
    public static void main( String[] args ){
        String s = "546";
        int result = NumberUtils.toInt(s);

    }
}

You can use NumberUtils class.

How to Convert Integer to String in Java?

Multiple ways we can convert Integer to String in java

  • String valueOf() method
  • Integer.toString() method
  • Append empty String

How to Convert Integer to String using valueOf() method in String

It returns the String version of the input integer and returns an integer.

Here is an example

public class App {
    public static void main( String[] args ){
        int i = 1234;
        String str = String.valueOf(i);

    }
}

How to Convert Integer to String using toString method

The toString method converts a numeric value into a string.

Here is an example

public class App {
    public static void main( String[] args ){
        int num = 1334;
        String result = Integer.toString(num);
    }
}

Output:

1334

if a string with a non-numerical value pass to the toString method, It throws an error java: incompatible types: java. lang.String cannot be converted to int. Here is an example

public class App {
    public static void main( String[] args ){
        String result = Integer.toString("num");
    }
}

Output:

java: incompatible types: java.lang.String cannot be converted to int

How to Convert Integer to String using Append empty String

We can convert to String method using append empty string. Add an integer with an empty string using the plus operator and returns the string.

public class App {
    public static void main( String[] args ){
        Integer i=123;
        String s=""+i;
    }
}

Conclusion

To Sum up, Learned multiple ways to convert String to/from Integer in java with code examples