Hit enter after type your search item

Definition of Java toString() method

In general, the 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:

java.lang.String;@36f72f09

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.

Note: All these classes (Integer, Float etc.) belong to java.lang.Object class. The toString is the method of the object class. So, all classes implement the methods of this class including arrays. In all above cases, the toString() method overrides the object class toString() method.

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:


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 see, 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 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:


The output:

javatst.fruits@281c35ec

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 purpose. The toString() can be used for getting 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 above example and override the toString() method to return fruit names as follows:


Now, the output is:

The Fruits are: Apple Banana Mango Strawberry

Using toString Java method for objects to String conversion

You may use the toString() method to get the string representation of numbers as well. 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:

See online demo and code


Java toString

You may read a detailed guide for converting integers, float and double to string by using different ways.

This div height required for enabling the sticky sidebar