First, the understanding of the variable number of formal parameters
1. Format: For the method of the shape parameter according to type ... Formal parameter name
2. The method of the deformable parameter is overloaded with the method of the same name
3. Variable number of parameters can be called from 0 to infinity.
4. Using arrays is consistent with parameters using variable number of form participation methods.
5. If a method has a variable number of parameters, it must be declared at the end of the method parameter.
Packageday04; Public classTestargs { Public Static voidMain (string[] args) {Testargs C=NewTestargs (); C.print ("Hello"); C.print ("Hello" + "World"); C.print (5, "Hello Method Reload! ");} Public voidPrint (String ... arr) { for(inti=0;i<arr.length;i++) {System.out.println (arr[i]); }} Public voidPrint (intj,string ... arrr) {System.out.println (j); for(inti=0;i<arrr.length;i++) {System.out.println (arrr[i]); }}}
Second, the method of parameter transfer
Parameter passing mechanism in Java: value passing mechanism
* 1) parameter is the basic data type
Packageday04;/** parameter passing mechanism in Java: value passing mechanism * 1) parameter is the base data type, passing the value of the argument to the parameter * 2) parameter is a reference data type*/ Public classTestargstransfer { Public Static voidMain (string[] args) {Testargstransfer TT=NewTestargstransfer (); inti=10; intJ=5; System.out.println ("I:" +i+ "J:" +j); //value of the interchange variable inttemp=i; I=J; J=temp; System.out.println ("I:" +i+ "J:" +j); Tt.swap (i, j);//i:5 j:10 No swap//The value of I is passed to M, and the value of J is passed to N. The operation is only within tt.swap () and does not affect I and J.System.out.println ("I:" +i+ "J:" +j);} Public voidSwapintMintN) {inttemp=l; M=N; N=temp; System.out.println ("M:" +m+ "N:" +N); }}
* 2) parameters are reference data types: The value of the argument's reference type variable (the first address of the entity object corresponding to the heap space) is passed to the reference variable of the formal parameter.
Packageday04;/** Values for reference data types are passed*/ Public classTestArgsTransfer1 { Public Static voidMain (string[] args) {TestArgsTransfer1 dd=NewTestArgsTransfer1 (); Dataswap GG=NewDataswap (); System.out.println ("GG.I:" +gg.i+ "GG.J:" +GG.J); Dd.swap (GG); System.out.println ("GG.I:" +gg.i+ "GG.J:" +GG.J); } Public voidswap (Dataswap tt) {inttemp=tt.i; TT.I=TT.J; TT.J=temp; }}classDataswap {inti=10; intJ=5;}
Java Learning Diary------Object-oriented ④