+ 3

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

Java

21st Jul 2021, 3:12 PM
alagammai uma
alagammai uma - avatar
4 Answers
+ 3
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 :)
21st Jul 2021, 5:36 PM
Tibor Santa
Tibor Santa - avatar
+ 2
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.
23rd Jul 2021, 8:23 AM
Arman saha
Arman saha - avatar
+ 1
Can args be used as parameter other than main Function?
21st Jul 2021, 3:20 PM
alagammai uma
alagammai uma - avatar
+ 1
Yes args is a type of variable only you can use it in other functions other than main method
21st Jul 2021, 5:01 PM
Atul [Inactive]