Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between String[] and String... in Java?

Tags:

java

arrays

main

How should I declare main() method in Java?

Like this:

public static void main(String[] args) {     System.out.println("foo"); } 

Or like this:

public static void main(String... args) {     System.out.println("bar"); } 

What's actually the difference between String[] and String... if any?

like image 703
Edward Ruchevits Avatar asked Aug 15 '12 16:08

Edward Ruchevits


People also ask

Is String [] the same as array String?

There's no difference between the two, it's the same. It says this in the docs: Array types can be written in one of two ways.

What does String [] mean in Java?

String [] arguments is a java array of String objects. This means that the main function expects an array of Strings. This array of strings typically holds all command line parameter arguments passed in when the program is run from the command line.

What is the difference between String [] args and String args?

String args[] and String[] args are identical. In the sense that they do the same thing, Creating a string array called args. But to avoid confusion and enforce compatibility with all other Java codes you may encounter I'd recommend using the syntax (String[] args) when declaring arrays.

Can we convert String [] to String?

So how to convert String array to String in java. We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.


Video Answer


2 Answers

How should I declare main() method in Java?

String[] and String... are the same thing internally, i. e., an array of Strings. The difference is that when you use a varargs parameter (String...) you can call the method like:

public void myMethod( String... foo ) {     // do something     // foo is an array (String[]) internally     System.out.println( foo[0] ); }  myMethod( "a", "b", "c" );  // OR myMethod( new String[]{ "a", "b", "c" } );  // OR without passing any args myMethod(); 

And when you declare the parameter as a String array you MUST call this way:

public void myMethod( String[] foo ) {     // do something     System.out.println( foo[0] ); }  // compilation error!!! myMethod( "a", "b", "c" );  // compilation error too!!! myMethod();  // now, just this works myMethod( new String[]{ "a", "b", "c" } ); 

What's actually the difference between String[] and String... if any?

The convention is to use String[] as the main method parameter, but using String... works too, since when you use varargs you can call the method in the same way you call a method with an array as parameter and the parameter itself will be an array inside the method body.

One important thing is that when you use a vararg, it needs to be the last parameter of the method and you can only have one vararg parameter.

You can read more about varargs here: http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

like image 165
davidbuzatto Avatar answered Oct 12 '22 12:10

davidbuzatto


String... gets converted to a String[]. The main difference is that you can call a vararg method in 2 ways:

method(a, b, c); method(new String[] {a, b, c}); 

whereas you need to call a method that accepts an array like this:

method(new String[] {a, b, c}); 

For the main method it does not make a difference.

like image 35
assylias Avatar answered Oct 12 '22 12:10

assylias