Mechanism analysis of passing time-value of parameter in Java

Source: Internet
Author: User

What is parameter passing? In the C function or Java method, pass a parameter to a function or method, for example: void Fun ( int num) {num+=2;} Int a= 3; Fun ( a) ; This a is passed as a parameter into the function fun (), as a, and then returns or does not return the value back to the original, the function is to reuse, then we want to pass this parameter what is it?      That is, if we remove the function of the cloak, let the function into the code before the function of the place, then it is natural that the value of the last B will be changed, this can be said to be the most simple parameter passed, natural appearance. But people have invented another way. In fact, I think this is a redefinition of the function, the function is only as a piece of code to the reuse of a tool, function is not as part of the call function code, but the function as a tool,     A function can only affect the calling code in a way that returns a value (this is not considered to be able to communicate with code other than parameters in the function body, because Java is flooded, but the value in Java is also the class rather than the calling code), which is more secure. The method used in this way is copy a new valueIn a function, it's like: a local variable initialized at the time of the call.     We know that local variables exist only within functions, so that operations inside the function do not affect the outside. This approach is called value passing. So how does it come true? Look underneath. It says to copy a new value, actually this thing has the doorway. Start with the hardware.     All the code in the run, the inside data are in memory, including files Ah what need to be removed from the hard disk SD card, put into memory, that is, all your values Ah, object Ah, all in memory. And the place in memory where these things are divided into two pieces: heap, Stack very threadbare plot: where the stack position is small, fast, the location of the heap is large, slow so it is natural to have a plan to put things, small data, with a lot of data, put the stack, big data, use less data, put the heap in the concrete in Java is such Of
    • There are 9 kinds of data types in Java, 8 basic types and 1 object types, and the object types are divided into system self-built and user self-built
    • The seven basic types of data such as int, long, double, float, Byte, Boolean, char are placed directly on the stack
    • The string base type and all the object types of data (where the data refers to the instantiated object, the empty class is not the data) placed in the heap, and the pointers stored in the heap on the stack
So if you want to say that a new value is being copied, the situation is different for only the data that exists in the stack and stored in the stack and heap. Say before you copy, what happens when you create a value? The same is true of two things:
    1. Create a data store in the stack
      1. For example: int a = 3; This sentence is actually two steps: int A; A = 3;
      2. The first step int A is found in the stack and opens up a storage space of type int, and then binds the storage space with the variable name a (the mechanism of my custom binding takes place in the namespace);
      3. The second step is to store the value of 3 in a bound storage space.
    2. Create a pointer to the data stored in the stack, where the contents are stored in the heap
      1. For example: Apple myapple = new Apple (); This sentence is also two steps: Apple myapple; Myapple = new Redapple ();
      2. The first step is to find the stack and open up a storage space, because the declaration is not a basic type, so it opens up a pointer type of storage space, and then the storage space with the object name myapple bound up;
      3. The second step is to (deliberately using polymorphic properties) first in the heap of memory to open up a space to hold the Redapple object (the space does not have a binding namespace), and then after the type check is legal, the address of the space is placed in the pointer space bound with Myapple
The state of the data in memory is like this: OK, back to the beginning, then copy a new value to the function of the copy of the operation of the actual situation is what it looks like? In the case of these two functions: the process of transmitting a parameter is actually a process of assigning a value:
    • Fun (a); When this sentence is executed, the argument is actually executed in two sentences: int num; num = a ;
      • an int type int size space is opened in the stack, and Num is bound, and then the data in the stack space of a bound is copied into the stack space of NUM bindings .
    • The same is true of Fun (myapple): Apple Apple; Apple = myapple;
      • Create an apple type pointer size space in the stack, bind Apple, and then copy the data (the address) of the myapple bound stack space into Apple-bound stack space
Now the data is in memory state like this: at the time of the parameter transfer, copy data from a bound stack space into num bound stack spaceBehavior is called value passing (also occurs when the = operator assignment operator). But you have to notice, you can also be very clear that, after the so-called value of the transfer, the function of the variable operation will not affect the call function code variables, is not necessarily, need to see whether the value or address, but also to see where the real data is placed, the real data in the heap is passed in the stack address, can be changed But in fact, if the incoming address, can change also depends on what the operation. Take the above apple.dosomesth () as an example:
    • If this method can operate on some of Apple's values, the contents of the Myapple in the code that invokes the function are also subject to change.
    • But if it is an assignment, it is the assignment in Java. So, it is still the value of the transfer, is changed in the local variable to new object of the stack space address, may be replaced by a new address, but the original address is linked to the heap space data is not affected, that is, the code that calls the function myapple content is completely unaffected

Mechanism Analysis of parameter pass-time value passing in Java

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.