5.3 The ToString () method of the object class

Source: Internet
Author: User

public class student {private  String name;private int age;public student ()  {super ();} Public student (String name, int age)  {super (); this.name = name;this.age  = age;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;}   @Override// public string tostring ()  {// // return super.tostring () ;// // return  "Hello";// return  "Name:"  + name +  ", Age:"  +  AGE;//&NBSP,} @Overridepublic  string tostring ()  {return  "Student [name="  +  name +  ",  age="  + age +  "]";}} 
/* * public string tostring (): Returns the string representation of the object. A static method under the  *  * integer class:  * public static string tohexstring (int  i): Turns an integer into a hexadecimal-represented string  *  *  but this information does not make any sense. Therefore, it is recommended that all subclasses override this method.  *  How to rewrite it? *  all member variable values of the class are returned. The final version of the  *  rewrite is to automatically generate the ToString () method.  *  *  Note that: *   directly outputs the name of an object, which is actually called the ToString () method of the object.  */public class studenttest {public static void main (String[] args)  {student s = new student (); System.out.println (S.hashcode ()); System.out.println (S.getclass (). GetName ()); System.out.println ("--------------------"); System.out.println (S.tostring ()); System.out.println ("--------------------"); the value of the// tostring () method is equivalent to// getclass (). GetName ()  +  ' @ '  + integer.tohexstring (hashcode ())// this.getclass (). GetName () + ' @ ' +integer.tohexstring ( This.hashcode ()) System.out.println (S.getclass (). GetName ()  +  ' @ ' + integer.tohexstring (S.hashcode ())); System.out.println (S.getclass (). GetName ()  +  "@" + integer.tohexstring (S.hashcode ())); System.out.println (S.tostring ());//  the name of the direct output object System.out.println (s);}}


5.3 The ToString () method of the object class

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.