Java Transmit value and address

Source: Internet
Author: User

When calling a function, the passed argument may have been passed in the past, or it might have been a pass-through. If the value is passed, the operation inside the function has no effect on the value of the parameter, and if it is a pass-through, the operation inside the function is to manipulate the memory pointed to by the parameter, affecting the value of the parameter.

Is Java really a value or a pass-through? Use the following example to experiment:

 Packagetest;Importjava.util.ArrayList;Importjava.util.List; Public classvalueandaddress { Public Static voidF1 (intx) {x= x + 5; return; }         Public Static voidF2 (String s) {s=NewString ("10000"); return; }         Public Static voidF3 (list<integer>list) {List.remove (0); List.remove (1); List.remove (2); return; }         Public Static voidMain (string[] args) {intx = 5; String s=NewString ("ABCdef"); List<Integer> list =NewArraylist<integer>(); List.add (0); List.add (1); List.add (2); List.add (3); List.add (4);        F1 (x);        F2 (s);                F3 (list); System.out.println ("X=" +x); System.out.println ("S=" +s);  for(inti=0; I<list.size (); i++) {System.out.println ("list[" + i + "]=" +List.get (i)); }            }}

Operation Result:

It can be seen that the value of the parameter of int and string is not changed, it is a pass value, and the parameter value of the list type has changed, is the address.

Java Transmit value and address

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.