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.
public class Demo
{
public static void main(String args[])
{
Integer i1 = new Integer(100);
int x = 10;
// to convert i1 to String
String integerString1 = i1.toString(); // 1st way
System.out.println(integerString1); // prints 100
String integerString2 = String.valueOf(i1); // 2nd way
System.out.println(integerString2); // prints 100
// to convert x to String
String intString1 = String.valueOf(x); // 1st way
System.out.println(intString1); // prints 10
String intString2 = Integer.toString(x); // 2nd way
System.out.println(intString2); // prints 10
}
}
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.
public class Demo
{
public static void main(String args[])
{
String str1 = Integer.toString(100, 2);
System.out.println("Binay representation of 100 is " + str1);
String str2 = Integer.toString(100, 8);
System.out.println("Octal representation of 100 is " + str2);
String str3 = Integer.toString(100, 16);
System.out.println("Hexadecimal representation of 100 is " + str3);
}
}
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.
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
Refer this link:
http://way2java.com/java-general/tostring-java/