Java toString() method is used to return the string or textual representation of an object.
The object class’s toString method returns a string as the name of the specified object’s class which is followed by ‘@’ sign and the hashcode of the object (unsigned hexadecimal representation).
For example:
The recommendation is to override the toString() class in subclasses to make it more meaningful. I will show you in the coming section.
- The Integer.toString() method returns the string representation of given integer numbers.
- The Float.toString() returns the string representation of the given float number. Alternatively, you can say, the toString() method converts a float to string value.
- Similarly, the Double class also has a toString() method for double numbers conversion to string.
- Even, String has a toString() method that returns itself i.e. the string object itself is returned.
I will show examples of Java toString() method for different uses in the following section.
An example of toString Java with Integer, Float, and String array
In this example, the toString method is used to return the string representation of an Integer array, a float array, and a string array.
The result is displayed that should be className@hashcode of the object.
Have a look:
public class toString_demo { public static void main(String args[]) { String[] strArr ={"x","y","z"}; Integer[] intArr = {10,15,20}; Float[] fltArr = {1.1f, 2.5f, 3.5f}; System.out.println("Output of String array: " +strArr.toString()); System.out.println("Output of Integer array: " + intArr.toString()); System.out.println("Output of Float array: " + fltArr.toString()); } }
The output:
Output of String array: [Ljava.lang.String;@1e311410
Output of Integer array: [Ljava.lang.Integer;@281c35ec
Output of Float array: [Ljava.lang.Float;@54182d86
You saw, the output displayed the specified object class along with its hashcode. For example, the integer object returned: java.lang.Integer, as such, the object intArr belongs to the Integer class.
An example of using custom class with toString()
See the following code where a custom class is created and toString() method is used to get its object’s string representation:
package javatst; class fruits { private String fruit1; private String fruit2; private String fruit3; private String fruit4; public fruits(String fru1, String fru2, String fru3, String fru4){ fruit1 = fru1; fruit2 = fru2; fruit3 = fru3; fruit4 = fru4; } } public class toString_demo { public static void main(String args[]){ fruits fru = new fruits("Apple","Banana","Mango","Strawberry"); System.out.println(fru.toString()); } }
Output:
Where javatst is the package name, fruits is the class name of the object instance and 281c35ec is the hashcode.
So what is the use of toString() in that way?
Well, the output in the above example is not meant for the end users. Then, where this information can be used?
The general use of toString() is debugging or for diagnostic purposes. The toString() can be used to get useful details about the objects that can be meaningful for developers.
Making it meaningful by overriding the toString() class
As mentioned earlier, all subclasses should override the toString() method (officially).
In the following Java program, we will use almost the same code as in the above example and override the toString() method to return fruit names as follows:
class fruits { private String fruit1; private String fruit2; private String fruit3; private String fruit4; public fruits(String fru1, String fru2, String fru3, String fru4){ fruit1 = fru1; fruit2 = fru2; fruit3 = fru3; fruit4 = fru4; } public String toString(){ return "The Fruits are: "+ fruit1 + " "+ fruit2 + " " + fruit3 + " " + fruit4; } } public class toString_demo { public static void main(String args[]){ fruits fru = new fruits("Apple","Banana","Mango","Strawberry"); System.out.println(fru.toString()); } }
Now, the output is:
Using toString Java method for objects to String conversion
You may use the toString() method to get the string representation of numbers.
Alternatively, we can say, the Java toString() method can be used for int to string, float to string, double to string, etc. conversion.
This can be useful if your string consists of numbers taken from different variables. In that case, the number can be converted to string and you may concatenate to create a combined or formatted string.
See an example below where six objects are created for the following types:
- Int and Integer
- Float and float
- Double and double
All these are converted into the string representation by using the toString() method:
public class toString_demo { public static void main(String args[]){ int numx = 100; Integer numy = 125; float fltx = 37.77f; Float flty = 60.88f; double dblx = 2145411.55d; Double dbly = new Double(454745474d); //converting numbers to string by toString() String str_int1 = Integer.toString(numx); String str_int2 = numy.toString(); String str_flt1 = Float.toString(fltx); String str_flt2 = flty.toString(); String str_dbl1 = Double.toString(dblx); String str_dbl2 = dbly.toString(); // Displaying after converting to strings System.out.println("int to string = " + str_int1); System.out.println("Integer to string = " + str_int2); System.out.println("float to string = " + str_flt1); System.out.println("Float to string = " + str_flt2); System.out.println("double to string = " + str_dbl1); System.out.println("Double to string = " + str_dbl2); } }
You may read a detailed guide for converting integers, float, and double to string by using different ways.