mutable parameters in Java

Source: Internet
Author: User

Java allows methods to use variable parameters after 1.5, the advantage of a mutable parameter is that it allows 0 or more parameters to be passed. For example, there is a code to download the following:

Java code 650) this.width=650; "class=" star "src=" Http://yizhenn.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border:0px; "/>

  1. Public class Test {

  2. Public Static void Main (string[] args) {

  3. Test ();

  4. Test (new string[]{"a"});

  5. Test (new string[]{"A", "B"});

  6. }

  7. Private Static void Test () {

  8. System.out.println ("[]");

  9. }

  10. Private Static void Test (string[] args) {

  11. System.out.println (arrays.tostring (args));

  12. }

  13. }


The code after using the mutable parameter is downloaded as follows:

Java code 650) this.width=650; "class=" star "src=" Http://yizhenn.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border:0px; "/>

  1. Public class Test {

  2. Public Static void Main (string[] args) {

  3. Test ();

  4. Test (new string[]{"a"});

  5. Test (new string[]{"A", "B"});

  6. }

  7. Private Static void Test (String ... args) {

  8. System.out.println (arrays.tostring (args));

  9. }

  10. }



> We know that in Java everything is a class except for 8 basic types. So what kind of variable parameter is it? Come, use the code download Verify!  

Java code 650) this.width=650; "class=" star "src=" Http://yizhenn.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border:0px; "/>

  1. Public class Test {

  2. Public Static void Main (string[] args) {

  3. Whatclass ();

  4. WhatClass1 ();

  5. }

  6. Private Static void whatclass (String ... args) {

  7. System.out.println (Args.getclass ());

  8. System.out.println (new string[]{}.getclass ());

  9. }

  10. Private Static void whatClass1 (int... args) {

  11. System.out.println (Args.getclass ());

  12. System.out.println (new int[]{}.getclass ());

  13. }

  14. }


The result of the above code is:
class [Ljava.lang.String;
class [Ljava.lang.String;
class [I
class [I


mutable 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.