Java Learning Notes-12. Passing and returning objects

Source: Internet
Author: User

The 1.Clone () method produces an object that must be assigned after the method has been used.

Vector v2 = (vector) v.clone ();

The 2.Clone () method is a protection type method in object, and if you create a class that requires the Clone () method, you need to write it yourself and inherit the Cloneable interface.

 PackageTweleve;ImportJava.util.*;classMyObjectImplementscloneable {inti; MyObject (intII) {i=II; }     PublicObject Clone () {Object Object=NULL; Try{Object=Super. Clone (); } Catch(clonenotsupportedexception e) {System.out.println ("MyObject can ' t clone"); }        returnobject; }     PublicString toString () {returninteger.tostring (i); }} Public classlocalcopy {StaticMyObject g (MyObject v) {V.I++; returnv; }    StaticMyObject F (MyObject v) {v=(MyObject) V.clone (); V.I++; returnv; }     Public Static voidMain (string[] args) {MyObject amyobject=NewMyObject (11); MyObject Bmyobject=g (Amyobject); if(Amyobject = =bmyobject) System.out.println ("A==b"); ElseSystem.out.println ("A!=b"); System.out.println ("A=" +amyobject); System.out.println ("B=" +bmyobject); MyObject CMyObject=NewMyObject (31); MyObject Dmyobject=f (cmyobject); if(CMyObject = =dmyobject) System.out.println ("C==d"); ElseSystem.out.println ("C!=d"); System.out.println ("C=" +CMyObject); System.out.println ("D=" +dmyobject); }}

3. The following steps are required if you want a class to be cloned: (1) Implementing the Cloneable interface

(2) Overwrite Clone () method

(3) Call Super.clone () in your own clone () method

(4) capturing violations in their clone ()

All the methods in the 4.String class that can modify string actually create and return a new string class, the original string class is not changed, including + and +=,stringbuffer more efficient than string manipulation strings.

 PackageTweleve;ImportJava.sql.Ref;Importorg.omg.CORBA.SystemException; Public classStringer {Staticstring UpCase (string s) {returns.touppercase (); }         Public Static voidMain (string[] args) {String qString=NewString ("Howdy");        System.out.println (qString); String QQ=upcase (qString);        SYSTEM.OUT.PRINTLN (QQ);    System.out.println (qString); }}

Java Learning Notes-12. Passing and returning objects

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.