The parameter-passing mechanism of Java function

Source: Internet
Author: User

One

A recent project, inside various return void, parameters with reference type, that called a ripe and many, but also confused me.

I'm going to take a good look at it. Java's communication mechanism, tidy up

Two

A lot of people listen to the Java pass, it will be blurted out, Java is the value of the pass . Congratulations, you got the answer.

So, what is the value of the pass?

If the parameter type is the original type , then a copy of the parameter is passed in, that is, the value of the original parameter , which is the same as the value that was previously discussed. If you change the value of a copy in a function, the original value is not changed .

If the parameter type is a reference type , then a copy of the reference parameter is passed in, and the copy holds the address of the parameter . If you do not change the address of the copy in the function, but instead change the value in the address, the changes within the function will affect 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, and the passed in parameter points to the original address, so the value of the parameter is not changed. threehere, I'll give you a code to understand.
1  Public classParamtertest {2      Public Static voidMain (string[] args) {3List<integer> List1 =NewArraylist<>();4List<integer> List2 =NewArraylist<>();5List1.add (0);6List2.add (0); (1)7 f (List1, list2);8System.out.println ("List1 2");9 p (list1);TenSystem.out.println ("List2 2"); One p (LIST2); //(4)  A     } -  -      Public Static voidF (list<integer> List1, list<integer>List2) { theList1.add (1); -List2.add (1); -List2 =NewArraylist<>(); //(2)  -List1.add (2); +List2.add (2); -System.out.println ("List1 1"); + p (list1); ASystem.out.println ("List2 1"); at p (LIST2); //(3)  -     } -  -      Public Static voidP (list<integer>list) { -          for(Integer i:list) { - System.out.println (i); in         } -     } to}

Output results

List1 1012201

Four

To be specific, it involves heaps, stacks, and method areas.

(1) List1,list2 just set up, all put into 0, because is an instance of the object so save in the heap inside .

(2) List1,list2 the address of his heap to the method, because it is referenced so stored in the stack , and through the address to the heap inside the instance, put into 1. However, at this point new instance, list2 in the stack no longer point to the heap instance, but a new address.

(3) List1,list2 put in 2, but here List1 still point to the heap instance , so put into 2, and List2 is the new address , heap list2 not put into 2.

(4) The method ends and the result of the return causes the heap's list1 to be put into 1, 2. List2 because there are new instances midway through the method, only 1 is put in, not 2.

Five

In fact, always confuse us, is that we judge the error is a string, is clearly a reference type, but is the basic type of characteristics, because the string is actually []char, specifically not detailed, I will lose a code, let everyone run.

 Public classParamterstringtest { Public Static voidMain (string[] args) {String S1= "a"; String S2= "a";        F (S1, S2); System.out.println ("S1 2");        P (S1); System.out.println ("S2 2");    P (S2); }     Public Static voidf (String s1, string s2) {S1= "B"; S2= "B"; System.out.println ("S1 1");        P (S1); System.out.println ("S2 1");    P (S2); }     Public Static voidp (String s) {System.out.println (s); }}

Similarly, the corresponding Interge and other packaging class.

The parameter-passing mechanism of Java function

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.