Home > Java > javaTutorial > body text

Example code for passing and returning objects in Java

零下一度
Release: 2018-05-24 13:58:56
Original
1500 people have browsed it

1. The Clone() method generates an object, and the object must be assigned after using the method.

Vector v2 = (Vector)v.clone();
Copy after login

2. The Clone() method is a protected type method in object. If the class you create needs to use the Clone() method, you need to rewrite it yourself 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");elseSystem.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");elseSystem.out.println("c!=d");
        System.out.println("c=" + cMyObject);
        System.out.println("d=" + dMyObject);

    }

}
Copy after login

3. If you want a class to be cloned, you need to follow the following steps: (1) Implement the Cloneable interface

                                            lone() method

                                                                                                                       (4) Capture violations in your own clone()

4. All objects in the String class Methods that can modify String actually create and return a new String class. The original String class has not changed, including + and +=. StringBuffer is more efficient than String when operating strings.

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);
    }

}
Copy after login

The above is the detailed content of Example code for passing and returning objects in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!