Last Updated : 05 Dec, 2018
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
// Java program to illustrate the
// toString() Method
import java.lang.*;
public class Geeks{
public static void main(String[] args) {
Integer obj = new Integer(8);
//It will return a string representation
String stringvalue1 = obj.toString();
System.out.println("String Value= " +
stringvalue1);
Integer obj3 = new Integer(10);
//It will return a string representation
String stringvalue3 = obj3.toString();
System.out.println("String Value = " +
stringvalue3);
}
}
Output:
String Value= 8 String Value = 10
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
// Java program to illustrate the
// toString(int a) method
import java.lang.*;
public class Geeks{
public static void main(String[] args) {
Integer obj = new Integer(8);
// It will return a string representation
// in base 8
String stringvalue1 = obj.toString(75);
System.out.println("String Value = " +
stringvalue1);
Integer obj2 = new Integer(8);
// It will return a string representation
// in base 2
String stringvalue2 = obj2.toString(6787);
System.out.println("String Value = " +
stringvalue2);
Integer obj3 = new Integer(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 = -787Program 2: For decimal and string parameters. Note: This results in an error and as well for the absence of suitable Integer constructor. java
// Java program to illustrate the
// Java.lang.Integer.toString(int a)method
import java.lang.*;
public class Geeks{
public static void main(String[] args) {
Integer obj = new Integer(8);
String stringvalue1 = obj.toString(58.5);
System.out.println("String Value = " +
stringvalue1);
Integer obj2 = new Integer(8);
String stringvalue2 = obj2.toString("317");
System.out.println("String Value = " +
stringvalue2);
// Empty constructor will result in an error
Integer obj3 = new Integer();
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
public static String toString(int a, int base)Parameter: The method accepts two parameters:
Input: a = 71, base = 2 Output: 1000111 Input: a = 314, base = 16 Output: 13aBelow programs illustrate the Java.lang.Integer.toString(int a, int base) Method: Program 1: java
// Java program to illustrate the
// toString(int, int) Method
import java.lang.*;
public class Geeks{
public static void main(String[] args) {
Integer a = new Integer(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 = 451Program 2: java
// Java program to illustrate the
// toString(int, int) Method
import java.lang.*;
public class Geeks{
public static void main(String[] args) {
Integer a = new Integer(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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4