println
println (Object x)
Print Object, and then terminate the line. This method first calls string.valueof (x) to get the string value of the printed object, and then behaves as if it first called print (string) and then calls println ().
Parameters:
X-the Object to print.
toString
toString ()
Returns a string representation of the object. Typically, the ToString method returns a string that represents this object in text format. The result should be a concise but easy to read information expression. It is recommended that all subclasses override this method.
The ToString method of the object class returns a string consisting of the class name (the object is an instance of the class), the at tag "@", and the unsigned hexadecimal representation of the object's hash code. In other words, the method returns a string whose value is equal to:
return:
The string representation of the object. Situation 1
Student class:
Package Com.zzu119.Testdemo;
/**
* Student Class
*
*/
public class Student {
Private String name = "Tom";
private int age = 20;
public void ToString1 () {
String str = ' name is ' + name + ', age is ' + age;
}
Public String toString2 () {
String str = ' name is ' + name + ', age is ' + age;
return str;
}
/* Public String toString () {
String str = ' name is ' + name + ', age is ' + age;
return str;
}*/
}
Test class:
Package Com.zzu119.Testdemo;
/**
* Student Test class
*
*/
public class Studenttest {
public static void Main (string[] args) {
Student stu = new Student ();
System.out.println (Stu);
}
}
The output is: The full name of the class
Situation 2
Student class
Package Com.zzu119.Testdemo;
/**
* Student Class
*
*/
public class Student {
Private String name = "Tom";
private int age = 20;
/* public void toString1 () {
String str = ' name is ' + name + ', age is ' + age;
}
Public String toString2 () {
String str = ' name is ' + name + ', age is ' + age;
return str;
}
*/
Public String toString () {
String str = ' name is ' + name + ', age is ' + age;
return str;
}
}
The test class is unchanged and the result is: