Println (Object) Tips

Source: Internet
Author: User
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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.