Parameter transfer in Java language Author: Chen yuefeng from: http://blog.csdn.net/mailbomb in the actual development process, method call is a very common operation, in the method call, many programmers may not understand how to process parameters clearly. The following describes the rules for passing parameters in Java, and some problems related to parameter transfer. Similar to other programming languages, Java parameter transfer is divided into two types: 1. By value transfer (by value) Applicability: 8 basic data types, String object features: copy a copy of data in the memory and pass the copied data to the method. The function is to change the parameter value inside the method, external data will not change. 2. By address. Applicability: array, all types of objects except string. features: to pass the object address to the method, modify the object content inside the method, and the external data will also change the basic sample code: public class test1 {public static void T1 (int n) {n = 10;} public static void T2 (string s) {S = "123 ";} public static void T3 (INT [] array) {array [0] = 2;} public static void Ma In (string [] ARGs) {int M = 5; T1 (m); system. out. println (m); string S1 = "ABC"; T2 (S1); system. out. println (S1); int [] arr = {1, 2, 3, 4}; T3 (ARR); system. out. println (ARR [0]) ;}} according to the above parameter transfer rule, the output result of the Code should be: 5 ABC 2. Because the int type is passed by value, when the Parameter m is passed to method T1, it is equivalent to copying another m value, the copied value is modified in Method T 1, so the m value remains unchanged. The output of S1 is similar to that of M. While arr is an array that is passed by address, that is, the address of ARR is passed to method T3. When the value in the array is modified in method T3, the original content also changes. The above features are defined in the Java language. In terms of syntax, it is impossible to specify whether parameter passing is passed by value or by address, but it can be implemented through the following Transformation: 1. If you need to modify the value of a parameter passed by value after the method call, you can use the return value. 2. For parameters passed by value, if you need to modify the original parameter within the method without changing it, You can recreate the object implementation within the method. The sample code is as follows: public class Test2 {public static int T1 (int n) {n = 10; return N;} public static string T2 (string s) {S = "123 "; return s;} public static void T3 (INT [] array) {// create a new array and assign a value to int [] newarray = new int [array. length]; // copy data to system. arraycopy (array, 0, newarray, 0, array. length); newarray [0] = 2;} public static void main (string [] ARGs) {int M = 5; // re-assign M = T1 (m); system. out. println (m); string S1 = "ABC"; // re-assign S1 = t2 (S1); system. out. println (S1); int [] arr = {1, 2, 3, 4}; T3 (ARR); system. out. println (ARR [0]) ;}} in this way, the output result of the program will be: 10 123 1. In actual program development, similar structures can be used as needed. Next, we will introduce a common application of parameter passing and use parameter passing to implement the return value. Such a function is widely used in read methods designed for Io classes. The sample code is as follows: public class test3 {public static void initarray (INT [] array) {for (INT I = 0; I <array. length; I ++) {array [I] = I ;}} public static void main (string [] ARGs) {int [] A = new int [10]; initarray (a); For (INT I = 0; I <. length; I ++) {system. out. println (A [I]) ;}} in this sample code, after the value of the array is modified inside the initarray method, the value of the external array a will also change, indirectly achieves the returned value. Of course, in this sample code, because only one parameter is returned, the effect is not obvious. If you need to return multiple parameters, it is a good idea to pass by address. Due to the rush of time, omissions are inevitable. Please actively add and correct them.