toString() Method In Java Example
Chapter:
String Handling
Last Updated:
21-07-2016 11:10:29 UTC
Program:
/* ............... START ............... */
public class JavaToStringExample {
int no;
String lang;
String other;
JavaToStringExample(int no, String lang, String other) {
this.no = no;
this.lang = lang;
this.other = other;
}
public static void main(String args[]) {
JavaToStringExample s1 = new JavaToStringExample(101, "Java", "Scan");
System.out.println(s1);// compiler writes here s1.toString()
}
}
/* ............... END ............... */
Output
Notes:
-
The toString() method returns the string representation of the object.
- toString() method by default will print [email protected] representation of hashcode.
- We can override this toString() method from object class.
- If we want to represent string representation of object then we need to override toString() method in our class and return values of the object as a String.
Tags
toString() Method, Java