Javatpoint Logo
Javatpoint Logo

Java Integer toString() Method

The toString() method of Java Integer class is used to get a String object representing the value of the Number Object. There are three different types of Java toString() method which can be differentiated depending on its parameter.

These are:

  1. Java Integer toString() Method
  2. Java Integer toString(int i) Method
  3. Java Integer toString(int i, int radix) Method

1. Java Integer toString() Method

The toString() method of Java Integer class returns a string equivalent of this Integer's value. This method gives the same result as Integer.toString(int i). It overrides the toString() method of class Object.

2. Java Integer toString(int i) Method

The toString(int i) is an inbuilt method of Java which is used to return a String object representing the specified integer in the method argument.

3. Java Integer toString(int i, int radix) Method

This method returns a string representation of the first int type argument in the radix specified by the second argument. If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX then the base 10 is used. In this method, the following ASCII characters are used as digits: 0 to 9 and a to z.

Note: If the first argument is negative, the result of the first element is the ASCII minus character '-'.

Syntax:

Following is the declarations of toString() method:

Parameter:

DataType Parameter Description Required/Optional
int i It is an integer value specified by the user to be converted to a string. Required
int radix This is of integer type and used in converting the string object. Required

Returns:

Method Returns
toString() It returns a string representation of the value of this integer object in base 10.
toString(int i) It returns a string representation of the int type argument in base 10.
toString(int i, int radix) It returns a string representation of the int type argument in the specified radix.

Exceptions:

NA

Compatibility Version:

Java 1.0 and above

Example 1

Test it Now

Output:

String Representation = 25
String Representation = -18

Example 2

Test it Now

Output:

Output: -12
Output: 12

Example 3

Output:

Enter Number to be string representation : -3465.876
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:939)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at myPackage.IntegerToStringExample3.main(IntegerToStringExample3.java:10)

Example 4

Test it Now

Output:

String Value = 65
String Value = 41
String Value = 101
String Value = 1000001

Example 5

Output:

Enter Inputs to be string representation: 
Enter Number: -45648
Enter Radix/Base: 16
Output: -b250





Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA