Integer.toString() method in Java with Examples

The toString() method of the Integer wrapper class converts an integer value to its string representation. This post will look at the toString() method in detail.

toString() method has three overloaded implementations in the Integer wrapper class –

Integer.toString() methods

Let’s look at the methods one by one.

public String toString()

toString() function is provided by the Object class itself, and every class extends the Object class. So, we also have a toString() method in the Integer wrapper class. The integer class overrides the toString() method to convert the underlying integer to a string.

  • What does it do? It will convert the integer value represented by the object to a string value.
  • What does toString() return? It will return a string representing the value of the integer.
Code Example –
public class Codekru {

	public static void main(String[] args) {

		Integer first = 123;

		// This will return the string repesentation of integer value
		String stringFirst = first.toString();
		System.out.println("String representation of first :" + stringFirst);

		// negative value integer
		Integer second = -123;

		// This will return the string repesentation of integer value
		String stringSecond = second.toString();
		System.out.println("String representation of second :" + stringSecond);

	}
}

Output –

String representation of first :123
String representation of second :-123

public static String toString(int i)

toString(int i) is a static method of the Integer class and thus called directly without making the object of the Integer class.

  • What does it do? It will take an integer into the argument and convert it into a string.
  • What does it return? It will return a string representation of the integer passed in the argument.

Please remember that Integer.toString() only converts the integer value, not the decimal value.

Code Example –
public class Codekru {

	public static void main(String[] args) {

		int positiveValue = 123;

		// converting the integer to string
		System.out.println(Integer.toString(positiveValue));

		int negativeValue = -123;

		// converting the integer to string
		System.out.println(Integer.toString(negativeValue));

	}
}

Output –

123
-123

Do you know public String toString() internally uses the static String toString(int i) to convert the integer to a string?

    public String toString() {
        return toString(value);
    }

public static String toString(int i, int radix)

toString() method converts an integer to the corresponding string by using the decimal system, or we can say it uses the radix or base as 10. But what if we wanted to convert an integer to a string using other radix values. In that case, we can use the toString(int i, int radix) to convert an integer into a string with the specified radix or base.

  • What does it do? It converts an integer to a string using the radix specified in the second argument.
  • What does it return? It returns a string representation of the first argument in the radix specified by the second argument.

Please note that the radix value can only be between 2 and 36 ( inclusive ). If we pass other values, the radix 10 would be considered the default value.

Code Example –
public class Codekru {

	public static void main(String[] args) {

		int positiveValue = 123;
		int radix = 16;

		// converting the integer to string
		System.out.println(
				"String representation of positiveValue with radix 16: " + Integer.toString(positiveValue, radix));

		int negativeValue = -123;

		// converting the integer to string
		System.out.println(
				"String representation of negativeValue with radix 16: " + Integer.toString(negativeValue, radix));

	}
}

Output –

String representation of positiveValue with radix 16: 7b
String representation of negativeValue with radix 16: -7b

The What If scenarios

Q – What if we try to use radix as 37 in the toString(int i, int radix) method?

In this case, the integer would be converted to a string using the radix as 10.

Below is the internal implementation of toString(int i, int radix), where radix 10 is considered when its value is less than 2 or greater than 36

 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
            radix = 10;

Here Character.MIN_RADIX = 2 and Character.MAX_RADIX = 36

public class Codekru {

	public static void main(String[] args) {

		int positiveValue = 123;
		int radix = 37; // radix > 36

		// converting the integer to string
		System.out.println(
				"String representation of positiveValue with radix 37: " + Integer.toString(positiveValue, radix));

		int negativeValue = -123;

		// converting the integer to string
		System.out.println(
				"String representation of negativeValue with radix 37: " + Integer.toString(negativeValue, radix));

	}
}

Output –

String representation of positiveValue with radix 37: 123
String representation of negativeValue with radix 37: -123
Q – What if we use the decimal values with the toString() method?

As toString() method only accepts an integer, so passing a decimal value will give a compilation error.

public class Codekru {

	public static void main(String[] args) {

		double positiveValue = 123.968; //passing double value

		// converting the integer to string
		System.out.println("String representation: " + Integer.toString(positiveValue));

	}
}

Output –

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method toString(int) in the type Integer is not applicable for the arguments (double)
Q – What if we use a null Integer with the toString() method?

We will get a NullPointerException as illustrated by the below example.

public class Codekru {

	public static void main(String[] args) {

		Integer i = null;

		// converting the integer to string
		System.out.println("String representation: " + Integer.toString(i));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "i" is null

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *