Java Learning--a method of changing the number of formal parameters

Source: Internet
Author: User

* Method of variable number of formal parameters:
* 1. Format: For method parameters: Data Type ... Formal parameter name
* 2. The method of a variable number of parameters forms an overload between the method with the same name
* 3. Variable number of formal parameters at the time of invocation, the number starts from 0, to infinitely many can.
* 4. The use of variable multiple formal parameters is consistent with the use of arrays for method parameters.
* 5. If there is a variable number of parameters in the method, be sure to declare at the end of the method parameter.
* 6. In one method, declare at most one variable number of parameters.
 Public classTestargs { Public Static voidMain (string[] args) {Testargs T=NewTestargs ();        T.sayhello (); //T.sayhello (New string[]{"Hello China", "Hello Beijing"});T.sayhello ("Hello China", "Hello Beijing"); }        //The following four methods form an overload//once the method of the overloaded variable number of parameters is defined in the class, the following two methods can be omitted//Public void SayHello () {//System.out.println ("Hello world!");//    }//Public void SayHello (String str1) {//System.out.println ("Hello" + str1);//    }    //method of parameter with variable number     Public voidSayHello (String ... args) { for(inti = 0;i < args.length;i++) {System.out.println (Args[i]+ "$"); }        //System.out.println ("=====");    }         Public voidSayHello (inti,string ... args) {    //Public void SayHello (String ... args,int i) {System.out.println (i);  for(intj = 0;j < args.length;j++) {System.out.println (Args[j]+ "$"); }    }         Public voidsayHello1 (string[] args) { for(inti = 0;i < args.length;i++) {System.out.println (args[i]); }    }        //examples of the use of variable number parameters//public int Getsum (int i,int j) {//return i + J;//    }//public int getsum (int i,int j,int k) {//return i + j + K;//    }     Public intGetsum (int.. args) {        intsum = 0;  for(inti = 0;i < args.length;i++) {sum+=Args[i]; }        returnsum; }        }

Java Learning--a method of changing the number of formal 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.