Java record-78-variable parameters

Source: Internet
Author: User

Java record-78-variable parameters
Varargs allows programmers to declare a method that accepts variable parameters. Note: variable parameters must be the last parameter declared by the method. Variable parameters are essentially an array. For a declared variable parameter, we can either pass a discrete value or an array object. However, if we define the parameters in the method as an array, we can only pass the array object but not the discrete value. A variable parameter must be the last parameter of a method parameter, that is, a method cannot have two or more variable parameters.

Public class vararstest {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); ret = sum (new int [] {1, 7}); // int... it is equivalent to int [] System. out. println (ret );}}

 


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.