Reference Delivery & Value passing

Source: Internet
Author: User

The following procedure illustrates the difference between value passing and application passing.

Package com.liaojianya.chapter1;/** * The demonstrates the use of the array reference. * @author LIAO Jianya * July 21, 2016 */public class Arrayreference{public static void Main (string[] args) {int x = 100;int AR R[] = {1, 2, 3, 4, 5, 6}; System.out.println ("----------before invoking Changerefervalue method-----------");p rint (x, arr); Changerefervalue ( x, arr); System.out.println ("----------after invoking Changerefervalue method-----------");p rint (x, arr);} public static void Changerefervalue (int A, int[] chgarr) {A + = 1;chgarr[0] = 0;chgarr[1] = 0;chgarr[2] = 0;} public static void Printarr (int[] arr) {for (int i:arr) {System.out.print (i + "");} System.out.println ();} public static void print (int x, int[] arr) {System.out.println ("x =" + x); System.out.print ("arr:");p Rintarr (arr);}}

Operation Result:

Analysis:

1) because the integer parameter A and the argument x is a value transfer relationship, so do not change the value of x itself, but in the Changerefervalue method, the value of x 100 is assigned to A,a + = 1, after a plus 1, the X has no effect.

2) Any modification to the array data pointed to by the parameter arr will synchronize the array data pointed to by the argument arr in the main method, which is an English-language reference where both the argument and the formal parameter point to the same block of memory space.

Reference Delivery & Value passing

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.