Java learning notes-12. Passing and returning objects, learning notes-12

Source: Internet
Author: User

Java learning notes-12. Passing and returning objects, learning notes-12

1. The Clone () method generates an object. After the method is used, the value of the object must be assigned.

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

2. the Clone () method is a protection type method in the object. If you need to use the Clone () method to create a class, you need to write it again and inherit the Cloneable interface.

package tweleve;import java.util.*;class MyObject implements Cloneable {    int i;    MyObject(int ii) {        i = ii;    }    public Object clone() {        Object object = null;        try {            object = super.clone();        } catch (CloneNotSupportedException e) {            System.out.println("MyObject can't clone");        }        return object;    }    public String toString() {        return Integer.toString(i);    }}public class LocalCopy {    static MyObject g(MyObject v) {        v.i++;        return v;    }    static MyObject f(MyObject v) {        v = (MyObject) v.clone();        v.i++;        return v;    }    public static void main(String[] args) {        MyObject aMyObject = new MyObject(11);        MyObject bMyObject = g(aMyObject);        if (aMyObject == bMyObject)            System.out.println("a==b");        else            System.out.println("a!=b");        System.out.println("a=" + aMyObject);        System.out.println("b=" + bMyObject);        MyObject cMyObject = new MyObject(31);        MyObject dMyObject = f(cMyObject);        if (cMyObject == dMyObject)            System.out.println("c==d");        else            System.out.println("c!=d");        System.out.println("c=" + cMyObject);        System.out.println("d=" + dMyObject);    }}

3. to clone a class, perform the following steps: (1) Implement the Cloneable interface.

(2) overwrite the Clone () method

(3) Call super. Clone () in your own clone () method ()

(4) capture the violation in your own clone ()

4. all the methods that can modify the String in the String class are created and a new String class is returned. The original String class is not changed, including ++ and ++ =, stringBuffer is more effective than String operations.

package tweleve;import java.sql.Ref;import org.omg.CORBA.SystemException;public class Stringer {    static String upcase(String s){         return s.toUpperCase();    }        public static void main(String[] args){        String qString=new String("howdy");        System.out.println(qString);        String qq=upcase(qString);        System.out.println(qq);        System.out.println(qString);    }}

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.