Java parameter value transfer:
Characteristic:
1. Parameters belong to local variables;
2. When an object calls a method, the parameters are allocated memory space, and the object is required to pass values for parameters to use. That is, when a method is called, the parameter variables must have specific values;
The following code demonstration: in the code, pv.tests() is wrong. When the object calls the method, it does not pass values for the parameters;
public class Pass_Value_Test { int tests(int x,int y){ int sum = x+y ; return sum; } public static void main(String[] args) { //Calculate the value of x+y Pass_Value_Test pv = new Pass_Value_Test(); System.out.println(pv.tests(1, 10)); //Correct System.out.println(pv.tests()); //error }
pv.tests() reports an error: the method tests (int, int) in the type pass ﹣ value ﹣ test is not applicable for the arguments()
1, Transfer value of basic data type parameter
The type level of the basic data type parameter cannot be higher than that of the parameter. For example, the value of float type cannot be used to transfer the value to the parameter of int type;
The following code demonstration: 11.11f and 111.111f are higher than int x and int y, so they report errors;
public class Pass_Value_Test { int tests(int x,int y){ int sum = x+y ; return sum; } public static void main(String[] args) { //Calculate the value of x+y Pass_Value_Test pv = new Pass_Value_Test(); System.out.println(pv.tests(1, 10)); //Correct pv.tests(11.11f, 111.111f); //error }
pv.tests(11.11f, 111.111f);
Error reporting: the method tests (int, int) in the type pass? Value? Test is not applicable for the arguments (float, float)
2, Pass value of reference type parameter
Java reference data types include: arrays, objects, interfaces, etc.; when parameters are reference types, "pass value" passes references stored in variables, not entities referenced by variables;
The following code is demonstrated with reference type parameters as objects:
//Create battery class class battery { int electricityAmount ; //Create a member variable to represent the remaining battery capacity battery(int amount){ //Parameter int type amount electricityAmount = amount ; } } //Create radio class, radio class radio { //Claim method, calculate battery usage void openRadio(battery bt){ //Create reference type parameter: bt object, at this time, the object is only declared, without reference and entity bt.electricityAmount = bt.electricityAmount - 10 ; //Every time the openRadio method is called, the power is reduced by 10 } } //Create main class public class Pass_Value_Test { public static void main(String[] args) { //Test the pass value of reference type parameter //Create three classes, a Battery class, a Radio class, and a test class battery bas = new battery(100); //Set the initial power to 100; System.out.println("The battery capacity is:"+bas.electricityAmount); System.out.println("Start using the radio·····"); radio ras = new radio(); //Assign the bas object to the reference type parameter bt object. At this time, bt has the reference of BAS and the entity of bas; ras.openRadio(bas); System.out.println("Turn off the radio, the battery power is left:"+ bas.electricityAmount); } }
In the above code, the reference type parameter bt has the same reference and entity as bas when calling ras.openRadio(bas); so it is in progress,
bt.electricityAmount = bt.electricityAmount - 10; during the statement, the value of the original entity electricityAmount in bas will also be changed;
That is to say, changing the entity referenced by the parameter variable will cause the same change to the entity of the original object;
It should be noted that:
Changing the reference stored in the parameter will not affect the reference stored in the variable to which the value is passed;
The following code is demonstrated:
//Create battery class class battery { int electricityAmount ; //Create a member variable to represent the remaining battery capacity battery(int amount){ //Parameter int type amount electricityAmount = amount ; } } //Create radio class, radio class radio { //Claim method, calculate battery usage void openRadio(battery bt){ //Create reference type parameter: bt object, at this time, the object is only declared, without reference and entity bt.electricityAmount = bt.electricityAmount - 10 ; //Every time the openRadio method is called, the power is reduced by 10 //Output the reference of bt at this time System.out.println("bt Quotation="+bt); } } //Create main class public class Pass_Value_Test { public static void main(String[] args) { //Test the pass value of reference type parameter //Create three classes, a Battery class, a Radio class, and a test class battery bas = new battery(100); //Set the initial power to 100; System.out.println("bas Quotation="+bas); System.out.println("The battery capacity is:"+bas.electricityAmount); System.out.println("Start using the radio·····"); radio ras = new radio(); ras.openRadio(bas); //Assign the bas object to the reference type parameter bt object. At this time, bt has the reference of BAS and the entity of bas; System.out.println("Turn off the radio, the battery power is left:"+ bas.electricityAmount); battery bax = new battery(100); //Set the initial power to 100; System.out.println("bax Quotation="+bax); ras.openRadio(bax); ras.openRadio(bax); System.out.println("bax Of electricityAmount: "+ bax.electricityAmount); System.out.println("bas Of electricityAmount: "+ bas.electricityAmount); } }
In the above code, when ras.openRadio(bax) is executed, the reference of bt parameter is changed. At this time, the reference and entity of bt are different from that of bas. Both the reference in BAS or the original value will not be changed due to the change of bt;
Code execution result:
Reference to bas = Chapter_Four.battery@7852e922
Battery capacity: 100
Start using the radio·····
Reference of bt = Chapter_Four.battery@7852e922
Turn off the radio, battery power remaining: 90
bax's reference = Chapter_Four.battery@4e25154f
Reference of bt = Chapter_Four.battery@4e25154f
Reference of bt = Chapter_Four.battery@4e25154f
electricityAmount of bax: 80
electricityAmount of bas: 90
Variable parameters:
1. Variable parameter refers to: when declaring a method, the name and number of parameters in the parameter list are not given, but the type of these parameters must be the same;
2. Variable parameters use "..." to represent several parameters, and the last parameter must be the last parameter in the parameter list of the method
Here is a demonstration:
Public void test (int... X) {} / / correct
Public void testone (double s, int... Y) {} / / correct
public void errorone (int... X, double y) {} / / error, because the last parameter Y does not belong to the last parameter of int... X in the parameter list
public void errordwo (int.. X) {} / / error because variable parameters use "..." to represent several parameters
Where x and y in int... X and int... y are "parameter Representatives" of variable parameters in the parameter list;
3. Parameter representation can represent specific parameters in parameter list by subscript operation, i.e. x[0], x[1], x[2], x[n-1] and x[n] respectively represent the first to nth parameters represented by X;
The following code is shown:
public class Parameter_Test { int sum = 0; // Variable parameter test void testx(int ... x) { for(int i=1;i<x.length;i++){ sum += x[i]; } System.out.println("sum Value="+sum); } public static void main(String[] args) { Parameter_Test pt = new Parameter_Test(); pt.testx(1,11,111,1111); pt.testx(2,22,222,2222,22222); } }
Code output:
sum = 1233
sum = 25921
In the above code:
Pt.testx (1,111111111); equivalent to four parameter lists in method testx, namely x[0], x[1], x[2], x[3]
Pt.testx (2,22222222222); equivalent to five parameter lists in method testx, namely x[0], x[1], x[2], x[3], x[4]
4. Java also provides an enhanced for loop, as shown in the following code:
public class Parameter_Test { int sum = 0; // Variable parameter test void testx(int ... x) { for(int i:x ){ sum += i; } System.out.println("sum Value="+sum); } public static void main(String[] args) { Parameter_Test pt = new Parameter_Test(); pt.testx(1,11,111,1111); pt.testx(2,22,222,2222,22222); } }
Code output:
sum = 1233
sum = 25921