Home > Java > javaTutorial > body text

Detailed explanation of examples of variable parameters in Java

零下一度
Release: 2017-07-03 09:55:17
Original
1759 people have browsed it

Everyone knows that the parameter of the main method is an array type, so it can actually be changed to an indefinite parameter type. I gave it a try and called some methods that popped up.

public class ClassC2 {public static void main(String...args) {
        
        System.out.println(args.getClass()); //Exception in thread "main" class [Ljava.lang.String;System.out.println("======分割线1=====");
        
        System.out.println(args.hashCode()); //4126736System.out.println("======分割线2=====");
        
        System.out.println(args.toString());
        System.out.println("======分割线3====="); //[Ljava.lang.String;@3ef810        
        System.out.println(args.length); //0System.out.println("======分割线4=====");
        
        args.notify(); //java.lang.IllegalMonitorStateException    }
}
Copy after login

Well, I replaced the "[ ]" after Sting with three dots (...). These three dots represent variable array parameters in Java. Variable argument type, also known as variable argument type. The English abbreviation is varargus. In other words, when this method receives parameters, the number is uncertain.

public class ClassC {public static void main(String[] args) {

        String[] array = { "Java", "PHP", "C#" };//传数组test(array);//调用    }static void test(String... a) { //利用可变参数的栗子System.out.println(a);// 打印:[Ljava.lang.String;@da6bf4System.out.println(a.length);//3for (int i = 0; i < a.length; i++) { // for遍历System.out.print(a[i]+"\t");//方便记录,我用了让它一行输出的效果:Java    PHP    C#        }

    }
}
Copy after login
//不像上面那样调用也可以,下面再演示几种调用方法public class ClassC {public static void main(String[] args) {
    test();//没有赋值,直接调用    }static void test(String... a) {  //利用可变参数的栗子System.out.println(a);// 打印:[Ljava.lang.String;@1e58cb8System.out.println(a.length);//0for(String s:a){  //复习一下增强for            System.out.println(s);
        }

    }
}
Copy after login
//不像上面那样调用也可以,下面再演示几种调用方法public class ClassC {public static void main(String[] args) {        // new一个空数组test(new String[0]);
    }static void test(String... a) { // 利用可变参数的栗子System.out.println(a);// 打印:[Ljava.lang.String;@1e58cb8System.out.println(a.length);//0for (String s : a) { // 复习一下增强for            System.out.println(s);
        }

    }
}
Copy after login
//不像上面那样调用也可以,下面再演示几种调用方法public class ClassC {public static void main(String[] args) {
        
        test(new String[]{"张三","李四","王五"});
    }static void test(String... a) { // 利用可变参数的栗子System.out.println(a);// 打印:[Ljava.lang.String;@b9e45aSystem.out.println(a.length);//3for (String s : a) { // 复习一下增强forSystem.out.print(s+"\t");//张三    李四    王五            }

    }
}
Copy after login

It can be seen that the parameters are defined as variable parameters, there are two A bit:

1. Don’t worry about catering to the previously defined parameters when calling in the future;

2. Didn’t think of it

(I just want these, welcome to add )

The above is the detailed content of Detailed explanation of examples of variable parameters in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template