Java IntegertoString Examples

In this tutorial, we will learn about Java Integer.toString(), Integer.toString(int) and Integer.toString(int, radix) methods, and learn how to use these method to get the string representation of given integer, with the help of examples.

toString

Integer.toString() returns a String object representing this Integer’s value.

Syntax

The syntax of toString() method is

Integer.toString()

Returns

The method returns value of type String.

Example 1 toString

In this example, we will take an integer object and get its string value using Integer.toString() method.

Java Program

public class Example {
	public static void main(String[] args){
		Integer integer = 5;
		String result = integer.toString();
		System.out.println("Result of toString() = " + result);
	}
}

Output

Result of toString() = 5

toStringint i

Integer.toString(i) returns a String object representing the specified integer i.

Syntax

The syntax of toString() method with int as parameter is

Integer.toString(int i)

where

Parameter Description
i The integer whose string representation has to be found.

Returns

The method returns value of type String.

Example 2 toStringint i

In this example, we will take an int i and get its string value by passing this int value as argument to Integer.toString() method.

Java Program

public class Example {
	public static void main(String[] args){
		int i = 5;
		String result = Integer.toString(i);
		System.out.println("Result of toString(" + i + ") = " + result);
	}
}

Output

toString(int i)

toStringint i, int radix

Integer.toString(i, radix) returns a string representation of the first argument i in the radix specified by the second argument radix.

Syntax

The syntax of toString() method with integer and radix as parameters is

Integer.toString(int i, int radix)

where

Parameter Description
i The integer whose string representation has to be found.
radix The radix value.

Returns

The method returns value of type String.

Example 3 toStringi, radix

In this example, we will string representation of an integer i = 24 in the radix specified by the specified radix = 3.

Java Program

public class Example {
	public static void main(String[] args){
		int i = 24;
		int radix = 3;
		String result = Integer.toString(i, radix);
		System.out.println("Result of toString(" + i + ", " + radix + ") = " + result);
	}
}

Output

Result of toString(24, 3) = 220

Example 4 radix 16 Hexadecimal

In this example, we will string representation of an integer i = 24 in the radix specified by the specified radix = 16. The value of radix is 16 which means we will get hexadecimal representation.

Java Program

public class Example {
	public static void main(String[] args){
		int i = 24;
		int radix = 16;
		String result = Integer.toString(i, radix);
		System.out.println("Result of toString(" + i + ", " + radix + ") = " + result);
	}
}

Output

Result of toString(24, 16) = 18

Conclusion

In this Java Tutorial, we have learnt the syntax of Java Integer.toString() method, and also how to use this method with the help of Java example programs.