The only way to pass the parameters of Java is to pass values (by value)

Source: Internet
Author: User
Tags reference variable

The reason to take this problem out again, because we operate Java objects (object) are all through the object of the reference (object references), reference can be interpreted as pointers, easy for people to correspond to the C + + address above. There is no harm in discussing the discussion.

When you pass a basic data type variable, the direct value is not confusing, but when passing the object, it should be understood to pass the value of the object reference, but the value is not the object itself, but the object, that is, you let another reference point to the object.

--java passes the object reference by value.

Take the following simple procedure as an example:

Package test;

public class Blogtest
{
public void modifyaaa (int x, java.util.ArrayList list)
{
x + 5;
System.out.println ("x=====" + x);
List.add (New Java.util.Date ());
System.out.println (List.size ());
}

    public static void Main (string[] args)
    {
         blogtest blogtest = new Blogtest ();
        int i = 5;
        java.util.ArrayList list = new Java.util.ArrayList ();
        System.out.println ("i=======" + i);
        System.out.println ("list.size () = =" +list.size ());
        blogtest.modifyaaa (i, list);
        System.out.println ("i==new==" + i);
        System.out.println ("list.size () = =" + list.size ());
   }
}
/*
The result of the run is this
i=======5
list.size () ===0
x=====10
1
i==new==5
List.size () ==new== 1
*/

This indicates that the following events occurred during parameter passing: the int variable I was copied to int x, and the value of x in the MODIFYAAA () method does not affect the value of I in the main function, which is, of course, a variable survival interval that is well understood. But the list in the main function also changes after discovering that the reference to the list object is passed to the MODIFYAAA () method. That's what this is about. When passing an object reference, the formal parameter gets a reference copy of the argument object, and two references all point to the same list object, so the formal parameter changes the list and can also be reflected in the argument.




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.