3 dot in parameter in Java

+1 vote

Can someone explain the meaning of the 3 dots, that we always use in our main method in Java?

public static void main(String... args[]){
    // method body
}
Nov 22, 2018 in Java by misc.edu04
• 1,450 points
39,374 views

3 answers to this question.

+2 votes
Best answer

The "Three Dots" in java is called the Variable Arguments or varargs. It allows the method to accept zero or multiple arguments. Varargs are very helpful if you don't know how many arguments you will have to pass in the method.

For Example:

abc(); // Likely useless, but possible 
abc("x", "y", "z"); 
abc("xyz"); 
abc(new String[]{"x", "y", "z"});

But one thing you must not is, the argument that gets the ... must be the last in the method signature. In simpler terms, 

abc (int i, String... strings) is correct, whereas:

abc(String... strings, int i) is wrong.

To know more about Java, join our Java certification course online.

Thanks

answered Nov 22, 2018 by geek.erkami
• 2,680 points

selected Oct 23, 2019 by Kalgi
Simple and superb answer. Thanks
0 votes

The three dots (...) are used in a function’s declaration as a parameter. These dots allow zero to multiple arguments to be passed when the function is called. The three dots are also known as var args.

answered Dec 16, 2020 by Gitika
• 65,870 points
0 votes

It means that zero or more String objects (or a single array of them) may be passed as the argument(s) for that method.

See the "Arbitrary Number of Arguments" section here: http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html#varargs

In your example, you could call it as any of the following:

myMethod(); // Likely useless, but possible
myMethod("one", "two", "three");
myMethod("solo");
myMethod(new String[]{"a", "b", "c"});

Important Note: The argument(s) passed in this way is always an array - even if there's just one. Make sure you treat it that way in the method body.

Important Note 2: The argument that gets the ... must be the last in the method signature. So, myMethod(int i, String... strings) is okay, but myMethod(String... strings, int i) is not okay.

answered Dec 16, 2020 by Rajiv
• 8,910 points

Related Questions In Java

0 votes
2 answers

How can we pass a function as a parameter in java?

Java 8 and above Using Java 8+ lambda ...READ MORE

answered Aug 28, 2018 in Java by Daisy
• 8,120 points
403 views
0 votes
0 answers

What is "String args[]"? parameter in main method Java

I am a beginner in java and ...READ MORE

May 15 in Java by Kichu
• 19,040 points
29 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
2,403 views
0 votes
1 answer

Purpose of “String args[]” in the “psvm” of Java

Let me give you the complete explanation ...READ MORE

answered May 7, 2018 in Java by code.reaper12
• 3,500 points
487 views
0 votes
1 answer
0 votes
1 answer

Running Java program without main method.

This would have worked fine till Java ...READ MORE

answered Nov 21, 2018 in Java by Anoop
228 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 63,108 views
0 votes
1 answer

Sorting an ArrayList in Java

You can easily do this by simply ...READ MORE

answered May 5, 2018 in Java by geek.erkami
• 2,680 points
1,083 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 9, 2018 in Java by geek.erkami
• 2,680 points
1,258 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP