Java Object class

Source: Internet
Author: User
Tags shallow copy throwable

There are a number of method types in the object class, which are learned in several lessons:

 Public Booleanequals (Object o);//returns whether the calling object (this) is the same as the O object//return o = = this//Typically, you want to overwrite, determine whether two objects are the same type, and whether the encapsulated values are the same Public classstudent{PrivateString sname; Private intAge ; PrivateString Sno;  Public Booleanequals (Object o) {if(NULL= = O)return false; if(O.getclass () = = This. GetClass ()) {Student s=(Student) o; return  This. Sname.equals (S.sname) && This. Age = = S.age && This. Sno.equals (S.sno) }}}

Protected Object Clone () throws Clonenotsupportedexception

Protected Object Clone ()throwsclonenotsupportedexception//return a clone of this instrance//throws Clonenotsupportedexception, if this class does not write implements Cloneable, this exception will be thrown//This copy is a shallow copy, copying only the property values of the object, and for the reference property, simply copying the reference//Example Public classStudentImplementscloneable{ PublicString name;  Public intAge ;  Public Static voidMain (string[] args) {Student s=NewStudent (); S.name= "Zhangsan"; S.age= 18; Try{Student s2=(Student) S.clone ();            System.out.println (S.name);        System.out.println (S2.name); }Catch(Clonenotsupportedexception r) {}}}

Public String toString ()

 Public String toString () // returns:a string representation of the object. // The default is GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())

protected void Finalize () throws Throwable

protected void Finalize ()                  throws throwable                  // called before the virtual machine cleans up the garbage object

Split Line---------------------------------------------------------------------------------------------------------------

Java 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.