Integer toString() in Java

JavaObject Oriented ProgrammingProgramming

The toString() method of the java.lang.Integer returns a string object. The Integer class has three toString() methods. Let us see them one by one −

String toString()

Example

The java.lang.Integer.toString() method returns a String object representing this Integer's value. Let us now see an example −

 Live Demo

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      Integer i = new Integer(20);
      // returns a string representation of the integer value in base 10
      String retval = i.toString();
      System.out.println("Value = " + retval);
   }
}

Output

Value = 20

static String toString(int i)

The java.lang.Integer.toString(int i) method returns a String object representing the specified integer. Here, i is the integer to be converted.

Example

Let us now see an example −

 Live Demo

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      Integer i = new Integer(10);
      // returns a string representation of the specified integer in base 10
      String retval = i.toString(30);
      System.out.println("Value = " + retval);
   }
}

Output

Value = 30

static String toString(int i, int radix)

The java.lang.Integer.toString(int i, int radix) method returns a string representation of the first argument i in the radix specified by the second argument radix.If the radix is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the radix 10 is used instead.

Here, i is the integer to be converted, whereas radix is the radix to be used in the string representation.

Example

Let us now see an example −

 Live Demo

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      Integer i = new Integer(10);
      // returns a string representation of the specified integer with radix 10
      String retval = i.toString(30, 10);
      System.out.println("Value = " + retval);
      // returns a string representation of the specified integer with radix 16
      retval = i.toString(30, 16);
      System.out.println("Value = " + retval);
      // returns a string representation of the specified integer with radix 8
      retval = i.toString(30, 8);
      System.out.println("Value = " + retval);
   }
}

Output

Value = 30
Value = 1e
Value = 36
raja
Published on 26-Sep-2019 12:50:34
Advertisements