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:

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

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());
        }
}

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:

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:

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:

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);
        }
}

Java toString

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

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!