Variable length parameters in Java

Source: Internet
Author: User

1**2* Created by Lenovo on 2017/12/10.3*variable length parameters in Java4*/5  Public classreflect04 {6 7     //M1 has a comparable variable length parameter of type int8     //M1 The argument can be 0-n when called9      Public Static voidM1 (int... a) {TenSYSTEM.OUT.PRINTLN ("int variable-length parameter execution ...." +a); One     } A  -     //If there is a method that can be precisely matched, the method is called, and the variable-length parameter is no longer executed. -      Public Static voidM1 (inta) { theSYSTEM.OUT.PRINTLN ("Exact match execution ..." +a); -     } -  -     //variable-length parameters can be equated as an array +      Public Static voidm2 (String ... args) { -          for(inti=0;i<args.length;i++){ +System.out.println ("M2 all like the sport as follows:" +args[i]); A         } at     } -  -      Public Static voidM3 (Class ... args)throwsexception{ -  -          for(inti=0;i<args.length;i++){ -Class arg =Args[i]; in System.out.println (Arg.newinstance ()); -         } to     } +  -     //variable-length parameters can only occur once, and can only appear in the last digit of all parameters the     //Error instance: *     //Public static void M4 (String ... args,int i) {} $      Public Static voidMain (string[] args)throwsException {Panax Notoginseng  -         //Executive M1 theM1 (1);//call the M1 method for exact matching +M1 (); AM1 (A); theM1 (1,2,3,4); +  -         //Executive M2 $M2 ("Travel", "See the movie", "Swim", "play"); $  -         //Executive M3 -M3 (Date.class, Employlee.class); the  -         //M1 OutputWuyi         //M1 Precision output: Exact match execution ..... 1 the         //int type variable long parameter execution .... [[Email protected] -         //int type variable long parameter execution .... [[Email protected] Wu         //int type variable long parameter execution .... [[Email protected] -  About         //m2 Output $         //m2 are fond of sports as follows: Travel -         //m2 are fond of the following sports: watching movies -         //m2 are fond of sports as follows: Swimming -         //m2 are fond of sports as follows: Play A  +         //m3 Output the         //Sun Dec 21:59:58 CST -         //NULL parameter construction method execution ... .. $         //employlee{name= ' null '} the     } the}

Variable length parameters in Java

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.