Difference between args[] vs argv[] in java? | Sololearn: Learn to code for FREE!


Difference between args[] vs argv[] in java?


7/21/2021 3:12:30 PM

alagammai uma

6 Answers

New Answer


public static void main(String[] args) This is the typically used notation. The argument of the main method is an array of strings, that can come from the command line when the program is invoked. String[] is the type and args is the name of the parameter variable. For historical reasons there are several ways to declare arrays: String[] args String []args String args[] These forms are equally valid and they compile, but in Java the first form is favored because it gives the best readability. The name of the variable is just a convention, technically you can use String[] x too, but nobody does that :) Also since Java 5, this is also valid: String... args It is the vararg (variable arguments) notation that is similar to an array (from the perspective of the main method, it does not make any difference). But it is rarely used. People stick to convention :)


If you're talking about the main function parameter, conventionally, args is used (short for arguments) argv is used in C++ as argument variables. Maybe C++ coders accidentally used argv or prefer argv. You can name it whatever you want but args is used to prevent confusion. But you should always use args over anything else


yes, whatever makes your program more readable ;)


In Java, String[] argv and String args[] are bothformal parameters in the main method main, similar to the formal parameters in functions in C++, but here is an array of strings. Among them, argv and args are the names of the array. Most of the textbooks are args, you can also choose a name yourself.


Can args be used as parameter other than main Function?


Yes args is a type of variable only you can use it in other functions other than main method