Java record -78-variable parameters

Source: Internet
Author: User

Variable parameters (Varargs)

Mutable parameters enable programmers to declare a method that accepts a variable number of parameters. Note that the mutable parameter must be the last parameter of the method declaration.

A mutable parameter is essentially an array, and for a variable argument, we can either pass a discrete value or pass an array object. But if we define the parameters in the method as arrays, we can only pass the array object and not pass the discrete values.

A mutable parameter must be used as the last parameter of a method parameter, that is, a method cannot have two or more mutable parameters of more than two.

Public class varargstest {    public static int sum (int ...  nums) {        int sum = 0;         for (int num : nums) {             sum += num;        }         return sum;    }     Public static void main (String[] args) {        int  ret = sum (1,2,4,7);         system.out.println (ret);                 ret =  sum (1,7);         system.out.println (ret);          &nbSp;      ret = sum (new int[]{1,7});  //int ...   equivalent to INT[]        SYSTEM.OUT.PRINTLN (ret);     }}


Java record -78-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.