Integer toString() in Java

- The java.lang.Integer.toString() is an inbuilt method in Java which is used to return the String object representing this Integer’s value.
Syntax :
public static String toString()
Parameters: The method does not accept any parameters.
Return Value:The method returns the string object of the particular Integer value.
Below program illustrates the Java.lang.Integer.toString() method:
// Java program to illustrate the// toString() Methodimportjava.lang.*;ÂÂpublicclassGeeks{ÂÂpublicstaticvoidmain(String[] args) {   ÂInteger obj =newInteger(8);    Â   Â//It will return a string representation    Â   ÂString stringvalue1 = obj.toString();   ÂSystem.out.println("String Value= "+                           Âstringvalue1);    Â    Â       ÂInteger obj3 =newInteger(10);    Â   Â//It will return a string representation   ÂString stringvalue3 = obj3.toString();   ÂSystem.out.println("String Value = "+                           Âstringvalue3);    ÂÂ}}Output:String Value= 8 String Value = 10
- The java.lang.Integer.toString(int a) is an inbuilt method in Java which is used to return a String object, representing the specified integer in the parameter.
Syntax :public static String toString(int a)
Parameters: The method accepts one parameter a of integer type and refers to the integer needed to be converted to string.
Return Value: The method returns the string representation of the argument in a particular base.
Examples:
For base 8: Input: int a = 75 Output: "75" For base 10: Input: int a = -787 Output: "-787"
Below programs illustrate the Java.lang.Integer.toString(int a) method:
Program 1:// Java program to illustrate the// toString(int a) methodimportjava.lang.*;ÂÂpublicclassGeeks{ÂÂpublicstaticvoidmain(String[] args) {   ÂInteger obj =newInteger(8);    Â   Â// It will return a string representation       Â// in base 8   ÂString stringvalue1 = obj.toString(75);   ÂSystem.out.println("String Value = "+                           Âstringvalue1);    Â   ÂInteger obj2 =newInteger(8);    Â   Â// It will return a string representation       Â// in base 2   ÂString stringvalue2 = obj2.toString(6787);   ÂSystem.out.println("String Value = "+                           Âstringvalue2);    Â    Â       ÂInteger obj3 =newInteger(10);    Â   Â// It will return a string representation       Â// in base 10   ÂString stringvalue3 = obj3.toString(-787);   ÂSystem.out.println("String Value = "+                           Âstringvalue3);    ÂÂ}}ÂOutput:String Value = 75 String Value = 6787 String Value = -787
Program 2: For decimal and string parameters.
Note: This results in an error and as well for the absence of suitable Integer constructor.// Java program to illustrate the// Java.lang.Integer.toString(int a)methodimportjava.lang.*;publicclassGeeks{ÂÂpublicstaticvoidmain(String[] args) {   ÂInteger obj =newInteger(8);   Â   ÂString stringvalue1 = obj.toString(58.5);   ÂSystem.out.println("String Value = "+                           Âstringvalue1);    Â   ÂInteger obj2 =newInteger(8);   Â   ÂString stringvalue2 = obj2.toString("317");   ÂSystem.out.println("String Value = "+                           Âstringvalue2);    Â    Â   Â// Empty constructor will result in an error   ÂInteger obj3 =newInteger();   ÂString stringvalue3 = obj3.toString(-787);   ÂSystem.out.println("String Value = "+                           Âstringvalue3);    ÂÂ}}ÂOutput:
prog.java:8: error: incompatible types: possible lossy conversion from double to int String stringvalue1 = obj.toString(58.5); ^ prog.java:12: error: incompatible types: String cannot be converted to int String stringvalue2 = obj2.toString("317"); ^ prog.java:17: error: no suitable constructor found for Integer(no arguments) Integer obj3 = new Integer(); ^ constructor Integer.Integer(int) is not applicable (actual and formal argument lists differ in length) constructor Integer.Integer(String) is not applicable (actual and formal argument lists differ in length) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 3 errors - The java.lang.Integer.toString(int a, int base) is an inbuilt method in Java which is used to return a string representation of the argument a in the base, specified by the second argument base. If the radix/base is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the base 10 is used. The ASCII characters which are used as digits: 0 to 9 and a to z.
Syntax:public static String toString(int a, int base)
Parameter: The method accepts two parameters:
- a: This is of integer type and refers to the integer value that is to be converted.
- base: This is also of integer type and refers to the base that is to be used while representing the strings.
Return Value: The method returns a string representation of the specified argument in the specified base.
Examples:
Input: a = 71, base = 2 Output: 1000111 Input: a = 314, base = 16 Output: 13a
Below programs illustrate the Java.lang.Integer.toString(int a, int base) Method:
Program 1:// Java program to illustrate the// toString(int, int) Methodimportjava.lang.*;ÂÂpublicclassGeeks{ÂÂpublicstaticvoidmain(String[] args) {ÂÂÂ Â Â Â Â Â Â ÂInteger a =newInteger(10);Â Â Â Â ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 2Â Â Â ÂString returnvalue = a.toString(5254,2);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 8Â Â Â Âreturnvalue = a.toString(35,8);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 16Â Â Â Âreturnvalue = a.toString(47,16);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 10Â Â Â Âreturnvalue = a.toString(451,10);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);}}ÂOutput:String Value = 1010010000110 String Value = 43 String Value = 2f String Value = 451
Program 2:
// Java program to illustrate the// toString(int, int) Methodimportjava.lang.*;ÂÂpublicclassGeeks{ÂÂpublicstaticvoidmain(String[] args) {ÂÂÂ Â Â Â Â Â Â ÂInteger a =newInteger(10);Â Â Â Â ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 2Â Â Â ÂString returnvalue = a.toString(-525,2);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 8Â Â Â Âreturnvalue = a.toString(31,8);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 16Â Â Â Âreturnvalue = a.toString(28,16);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);ÂÂÂ Â Â Â// It returns a string representationÂÂ Â Â Â Â Â Â Â// in base 10Â Â Â Âreturnvalue = a.toString(29,10);Â Â Â ÂSystem.out.println("String Value = "+ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âreturnvalue);}}ÂOutput:String Value = -1000001101 String Value = 37 String Value = 1c String Value = 29



