String toString() Java

String toString() Java | In this blog, we will discuss the toString() method of the Java String class, It is the built-in method that is used to return the string itself. While using this method there is no actual conversion that takes place it just returns the string without any changes which is called implicitly.

Examples for the toString() method is as follows:-
1. String:- “Hii”
After using toString():- Hii
2. int:- 0090
After using toString():- 0090

The syntax for the toString() method is as follows:- public String toString()
Parameters:- there are no parameters passed to this method.
Return type:- string
Returns:- string.

String toString() Method in Java – Example

public class Main {
   public static void main(String args[]) {
      String string = "An apple a day keeps a doctor away";
      System.out.print("Output String Value: ");
      System.out.println(string.toString());

      String string1 = "Now a days apples are been coated by wax.";
      System.out.print("Output String Value: ");
      System.out.println(string1.toString());
   }
}

Output:-

Output String Value: An apple a day keeps a doctor away
Output String Value: Now a days apples are been coated by wax.

public class Main {
   public static void main(String[] args) {
      String string = Integer.toString(52);
      System.out.println(string);
      System.out.println(string.toString());
   }
}

Output:-

52
52

Let us see it through an Object. The below defined Student object contains variables rollno, name, and city. Among them, name and city are string types. When we display the student object then the print() or println() method internally calls the toString method and it will invoke the String.toString() method to display the string values. The below example demonstrates it.

public class Student {
   int rollno;
   String name;
   String city;

   Student(int rollno, String name, String city) {
      this.rollno = rollno;
      this.name = name;
      this.city = city;
   }

   @Override
   public String toString() {
      return "Student [rollno=" + rollno + 
             ", name=" + name + ", city=" + city + "]";
   }
}
public class Main {
   public static void main(String args[]) {
      Student string1 = new Student(1, "John", "Los Angeles");
      Student string2 = new Student(2, "Amelia", "Chicago");

      System.out.println(string1);
      System.out.println(string2);
   }
}

Output:-

Student [rollno=1, name=John, city=Los Angeles]
Student [rollno=2, name=Amelia, city=Chicago]

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.