Java variable parameters explained

Source: Internet
Author: User

If you implement multiple methods, the logic of these methods is basically the same, the only difference is the number of parameters passed, you can use the variable parameter
Variable parameter definition method data type ... The name of the array, which stores the arguments passed over, similar to JavaScript
Note the point:
(1) Variable parameters need to be written in the parameter list of the method and cannot be defined separately
(2) There can be only one variable parameter in the parameter list of a method
(3) A variable parameter in the parameter list of the method, which must be placed at the end of the argument
-Score (String name, int ... Args

Case

 Public classChangeableargs { Public Static voidMain (string[] args) {score ("Xiaoming", 90,80,70,90); Score ("Little Red", 90,80,70,90,100,200,300); System.out.println (Add (90,80,70,90)); System.out.println (Add (90,80,70,90,100,200,300)); }    Private Static voidScore (String name,int.. args) {        intResult=0;  for(inti=0;i<args.length;i++) Result+=Args[i]; SYSTEM.OUT.PRINTLN (Name+ "Test the +result+" points! "); }    Private Static intAddint.. args) {        intResult=0;  for(inti=0;i<args.length;i++) Result+=Args[i]; returnresult; }}

Run:

Java variable parameters explained

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.