Java basics-variable Parameters

Source: Internet
Author: User

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

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.