Java parameter passing

Source: Internet
Author: User
Introduce the parameter transfer problem with one problem.

Public static void main (string [] ARGs ){
Int x = 1;
Int [] Y = new int [10];
M (x, y );
System. Out. println ("X is" + x );
System. Out. println ("Y [0] is" + Y [0]);
}
Public static void M (INT number, int [] numbers ){
Number = 1001;
Numbers [0] = 5555;
}
Result
X is 1
Y [0] Is 5555
Why does the X value not change to Y [0 ]?

Change Java to language parameter passing


Java programming language only supports value passing parameters.

Java parameters, whether of the original type or reference type, are transmitted as copies (another way is to pass the value, but it is better to pass the copy, the value is usually relative to the address ).

If the parameter type is the original type, it is a copy of the parameter, that is, the value of the original parameter, which is the same as the previous value. If the copy value is changed in the function, the original value is not changed.

If the parameter type is a reference type, it is a copy of the reference parameter that stores the parameter address. If the address of this copy is not changed in the function, but the value in the address is changed, the change in the function will affect the passed parameters. If a copy address is changed in the function, for example, a new one, the copy points to a new address. At this time, the input parameter still points to the original address, therefore, the parameter value is not changed.

Based on the theory, we will analyze the problem above.

Now your problem is clear.

From the memory perspective, Let's explain

When defining X.

Variable X --> [storage value 0]

Execute M (x, y); variable X copies the value 0 and points

Variable Number --> [store the copied value 0]

Then change the value of number to 1.

Number --> [storage value 1];

At this point, you will find that the number and X are completely irrelevant, so you can understand why X has not changed.

The following is an array.

When defining y, Y is an object, so y is actually an address pointing to the real array location.

Variable y --> [store array address y]

Execute M (x, y). The variable Y copies the value array address y and points it to numbers.

Variable numbers --> [copy the storage array address y]

At this point, you will find that the value numbers and Y are actually pointing to the same address, that is, the same object. Therefore, you will find that the assignment is successful.

In fact, if you try this code, you will find something more interesting.

       public static void m(int number , int[] numbers) {number=1001;numbers = new int[10];numbers[0]=5555;}

Here, numbers fails to assign values. Why?

Very simple.

When defining y, Y is an object, so y is actually an address pointing to the real array location.

Variable y --> [store array address y]

When M (X, Y) is executed; variable Y copies array address y and points

Variable numbers --> [copy the storage array address y]

Execute New int [10] and assign the address to the reference of numbers.

Variable numbers --> [new address of the new array]

At this time, you will find that the address reference of. numbers is different from the referenced address of Y. That is to say, the effect of the variable number is the same. Therefore, the assignment fails.

Java Storage

In fact, some people may be confused about the form in which objects are stored. The previous question is, when is the value passed, and when is the reference address value passed.

1. Both stack and heap are places where Java is used to store data in Ram. Unlike C ++, Java automatically manages stacks and stacks, and programmers cannot directly set stacks or stacks. 2. The advantage of stack is that the access speed is faster than the heap speed, second only to the registers directly located in the CPU. However, the disadvantage is that the data size and lifetime in the stack must be fixed, and there is a lack of flexibility. In addition, stack data can be shared. For details, refer to the 3rd point. The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told to the compiler in advance. The Java garbage collector will automatically collect the data that is no longer used. However, the slow access speed is due to the need to dynamically allocate memory during runtime. 3. There are two data types in Java.

I. One is the basic type (primitive
Types), there are 8 types, namely int, short, long, byte, float, double, Boolean, char (note that there is no basic type of string ). The definition of this type is defined in the form of int A = 3; long B = 255l;, which is called an automatic variable. It is worth noting that the automatic variable stores the literal value, not the instance of the class, that is, it is not the reference of the class, and there is no class here. For example, int A = 3; here, A is a reference pointing to the int type, pointing to the literal value of 3. Because of the size and lifetime of the nominal value data, we can see that (these nominal values are fixed in a program block, and the field value disappears after the program block exits). For the reason of speed, it exists in the stack. (It is special that the string will also be stored in the stack .)

II. The other is packaging data, such as integer,
String, double, and other classes that encapsulate the corresponding basic data types. All the data of these classes exist in the heap. Java uses the new () statement to display and tell the compiler that the class is dynamically created as needed during runtime, so it is flexible, however, the disadvantage is that it takes more time.

Iii. string is a special packaging data. You can use the string STR = new string ("ABC"); or the string STR = "ABC"; (for comparison, you have never seen integer before JDK 5.0.
I = 3;, because the class and the literal value are not generic, except string. In JDK 5.0, this expression is acceptable! Because the compiler converts integer I = new INTEGER (3) in the background ). The former is the process of creating a standardized class, that is, in Java, everything is an object, and the object is a class instance, all created in the form of new. Some Classes in Java, such as the dateformat class, can return a newly created class through the getinstance () method of the class, which seems to violate this principle. Actually not. This class uses the singleton mode to return the instance of the class, but this instance is created in the class through new (), and getinstance () hides this detail from the outside. Why is it in string
STR = "ABC"; does not use new () to create an instance, does it violate the above principles? Actually no.


In the parameters passed in Java, what is the literal value and the address value? In fact, it is very simple. The basic types are passed by the nominal value. that is, the value directly stored in the stack. the double, integer, and other custom objects all pass the object address. because they exist in the heap. the difference is that the double and integer types cannot be changed. you can only change the value by resetting the new integer. In this way, the address will still change, and the value in the mian method cannot be changed. why is Java A value transfer. we can see that the objects passed by Java are all values in the stack. java uses this method to avoid pointers. in C, we can compare the passing of such a parameter dog * D, but in Java, nothing is just dog D. that is to say, this type is not defined separately in Java. although the actual transfer is also the address. however, this statement is not referenced in Java. this avoids our own definition (I think this idea is just like garbage collection, and we don't need to define something ....) whether the value or reference is passed. therefore, there is no need to argue much about this issue. just take a look.
Related Article

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.