What do 3 dots next to a parameter type mean in Java?

前端 未结 12 1730
灰色年华
灰色年华 2020-11-22 00:27

What do the 3 dots following String in the following method mean?

public void myMethod(String... strings){
    // method body
}
12条回答
  •  梦谈多话
    2020-11-22 00:54

    • It means zero or multiple parameter of same data type.
    • It must be the last parameter of the constructor or function.
    • We can have only one of this type parameter in respective constructor or function.

    Example 1:

    public class quest1 {
    
        public quest1(String... mynum) {
            System.out.println("yee haa");
        }
    
        public static void main(String[] args) {
            quest1 q=new quest1();
    
            quest1 q1=new quest1("hello");
    
        }
    }
    

    Example 2:

    public class quest1 {
    
        public quest1(int... at) {
            System.out.println("yee haa");
        }
    
        public quest1(String... at) {
            System.out.println("yee haa");
        }
    
        public static void main(String[] args) {
            quest1 q=new quest1("value");
    
            quest1 q1=new quest1(1);
    
        }
    
        public void name(String ... s) {
    
        }
    }
    

    output:

    yee haa

    yee haa

提交回复
热议问题