toString() Java

toString() Java

toString() is overloaded 3 times in Integer class as follows.

1. public static java.lang.String toString();
2. public static java.lang.String toString(int);
3. public static java.lang.String toString(int, int);

The first one is used to convert Integer object into string, the second one is used to convert int primitive data type into string and the third is used to convert the first parameter int into a radix representation as given in second parameter that can be octal, binay or hexa. Following program illustrates.

valueOf() is a static method of String class that convert a primitive data type and an object into string form.

In the following program, the third toString(int, int) is used.

image

The second parameter 2, 8, 16 of toString() indicates the first parameter 100 is to be converted to binary, octal and hexadecimal.

Another program is available that converts int to binary, otcal and hexa directly – int to Binary, Octal, Hexa Java.

2 thoughts on “toString() Java

  1. Mahi

    Public Class Date
    {
    public static void main(String args[])
    {
    Date today = new Date();
    System.out.println(today);
    }
    }
    why output printing with @ symbol ? And same with integer

Leave a Reply

Your email address will not be published. Required fields are marked *