JAVA parameters do not have reference passes, only values are passed __java

Source: Internet
Author: User

original article address: http://www.cnblogs.com/clara/archive/2011/09/17/2179493.html

when an object is passed as a parameter to a method, this method can change the property of the object and return the result of the change, so is this a value pass or a reference pass?
A: It's value delivery. The Java programming language has only values that pass parameters. When an object instance is passed to a method as a parameter, the value of the parameter is a copy of the object's reference. Points to the same object, the contents of the object can be changed in the invoked method, but the object's reference (not a copy of the reference) is never changed.


Java parameters, whether they are of the original type or the reference type, are passed by a copy (there is another argument for the value, but the copy is better understood, the value is usually relative to the address). If the parameter type is the original type, then a copy of the parameter is passed, which is the value of the original parameter, which is the same as the previous value.     Changing the value of a replica in a function does not change the original value. If the parameter type is a reference type, then the copy of the reference parameter is passed, which holds the address of the parameter. If you do not change the address of this replica in the function, but change the value in the address, the change within the function affects the parameters passed in. If you change the address of a copy in a function, such as new, the copy points to a new address, at which point the incoming argument still points to the original address, so the value of the parameter is not changed.

public class Paramtest {public static void main (string[] args) {/** * Test 1:methods can ' t Modi
          FY numeric Parameters * * SYSTEM.OUT.PRINTLN ("Testing triplevalue:");
          Double percent = 10;
          System.out.println ("before:percent=" + percent);
          Triplevalue (percent);

          System.out.println ("after:percent=" + percent); /** * Test 2:methods can change the state of object parameters * * SYSTEM.OUT.PRINTLN ("\nt
          Esting triplesalary: ");
          Employee Harry = new Employee ("Harry", 50000);
          System.out.println ("before:salary=" + harry.getsalary ());
          Triplesalary (Harry);

          System.out.println ("after:salary=" + harry.getsalary ()); /** * Test 3:methods can ' t attach new objects to object parameters/System.out.println (
          "\ntesting swap:");
          Employee A = new Employee ("Alice", 70000); Employeeb = new Employee ("Bob", 60000);
          System.out.println ("before:a=" + a.getname ());
          System.out.println ("before:b=" + b.getname ());
          Swap (A, b);
          System.out.println ("after:a=" + a.getname ());
    System.out.println ("after:b=" + b.getname ());
        private static void Swap (employee x, employee y) {Employee temp = x;
        X=y;
        Y=temp;
        System.out.println ("End of method:x=" + x.getname ());
    System.out.println ("End of method:y=" + y.getname ());
        private static void Triplesalary (Employee x) {x.raisesalary (200);
    System.out.println ("End of method:salary=" + x.getsalary ());
        private static void Triplevalue (Double x) {x=3*x;
    System.out.println ("End of Method x=" +x); }
}

Run Result:
Testing Triplevalue: before:percent=10.0 end of method
x= 30.0
after:percent=10.0

Testing Triplesalary:
before:salary=50000.0 end of
method:salary=150000.0
after:salary=150000.0

Testing Swap:
A copy of the Before:a=alice Before:b=bob end  of method:x=bob//visible reference is exchanged for end of
method:y=alice< C13/>after:a=alice  //reference itself is not exchanged
After:b=bob


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.