We all know that the Main method parameter is an array type, then it can be changed to variable parameter type. I tried and called out some of the pop-up methods.
public classClassC2 { public Static voidmain (string...args) {System.out.println (args.getclass ());//Exception in thread "main" class [Ljava.lang.String;System.out.println ("====== Split Line 1====="); System.out.println (args.hashcode ()); //4126736System.out.println ("====== Split Line 2====="); System.out.println (args.tostring ()); System.out.println ("====== Split Line 3=====");//[ljava.lang.string;@3ef810System.out.println (args.length);//0System.out.println ("====== Split Line 4====="); Args.notify (); //java.lang.IllegalMonitorStateException }}
well, I replaced the "[]" behind the sting with three dots (...). ), these three points represent variable array parameters in Java. A variable parameter type (variable argument type), also known as an indeterminate parameter type. The English abbreviation is Varargus. This means that the number of the method is variable when it receives the Parameter.
public classClassC { public Static voidmain (string[] Args) {string[] array= {"Java", "PHP", "c #"};//Pass ArrayTest (array);//called } Static voidTest (String ... a) {//chestnut with variable parametersSystem.out.println (a);//print: [Ljava.lang.String; @da6bf4System.out.println (a.length);//3 for(inti = 0; I < a.length; I++) {//for TraversalSystem.out.print (a[i]+ "\ t");//convenient to record, I used to let it line output effect: Java PHP C # } }}
//It is not possible to invoke it as above, but here are some more ways to call it public classClassC { public Static voidmain (string[] Args) {test ();//no assignment, call directly } Static voidTest (String ... a) {//chestnut with variable parametersSystem.out.println (a);//print: [ljava.lang.string;@1e58cb8System.out.println (a.length);//0 for(String S:a) {//Review the Enhancements forSystem.out.println (s); } }}
//It is not possible to invoke it as above, but here are some more ways to call it public classClassC { public Static voidmain (string[] Args) {//new an empty arrayTestNewString[0]); } Static voidTest (String ... a) {//chestnut with variable parametersSystem.out.println (a);//print: [ljava.lang.string;@1e58cb8System.out.println (a.length);//0 for(String S:a) {//Review the Enhancements forSystem.out.println (s); } }}
//It is not possible to invoke it as above, but here are some more ways to call it public classClassC { public Static voidmain (string[] Args) {test (NewString[]{"zhang san", "john doe", "harry"}); } Static voidTest (String ... a) {//chestnut with variable parametersSystem.out.println (a);//print: [Ljava.lang.String; @b9e45aSystem.out.println (a.length);//3 for(String S:a) {//Review the Enhancements forSystem.out.print (s+ "\ t");//Dick and Harry Harry } }}
This shows that the parameter is defined as a variable parameter, there are two a bit:
1. Do not worry about the future when calling, to meet the previously defined parameters;
2. Didn't want to come out
(i just want this, Welcome to Add)
Java basics-variable Parameters