Java Integer toString() MethodThe 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() MethodThe 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) MethodThe 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) MethodThis 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:
Returns:
Exceptions:NA Compatibility Version:Java 1.0 and above Example 1Output: String Representation = 25 String Representation = -18 Example 2Output: Output: -12 Output: 12 Example 3Output: 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 4Output: String Value = 65 String Value = 41 String Value = 101 String Value = 1000001 Example 5Output: Enter Inputs to be string representation: Enter Number: -45648 Enter Radix/Base: 16 Output: -b250 Next TopicJava-integer-tounsignedlong-method |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India