Reputation: 2864
I'm trying to print "Hello World" from a Java program, but I am a little confused with the main method:
public static void main(String[] args)
and
public static void main(String args[])
Both these functions perform the same task. How does this happen?
I know the function of String but not of the args.
Upvotes: 5
Views: 3969
Reputation: 1809
There is exactly no difference between the two, though according to the Java standards, the convention for making an array is:
String[] args
rather than,
String args[]
and equally valid form using the ellipses as well (only for infinite array):
String...args
A brief overview of the psvm() in Java:
public: the method can be accessed anywhere within the class or outside the class or even outside the package.
static: the method is shared by all the objects and it does not need any objects to be created to call this method.
void: this particular main() method cannot return any value so here we have to use void; it is the syntax which need to be followed.
main(): this is the entry point of the code or the class you execute. In most cases, this is also the exit point of the code.
String[]args: these are run-time commands line arguments, whether you pass them or not you have to mention them in the arguments.
Upvotes: 0
Reputation: 165
Im gonna try to explain all the parts of this line public static void main(String[] args)
main: its a Java convention inherited from C.. the first method to run its the one called main.
void: this method should return nothing, which in java its called void
static: you don't need to create an instance of this class to run this method(completely logic if is the first one to run)
public: it is possible to call this method from other classes, even outside of the package(again its logic been the firsts method to be called)
Now to your question String[] args is completely equivalent to String args[] or to String[] name... anyway, it means that args(or name in the last case) its the name of the array of Strings that the method is receiving. Its an array because of the symbols [] and in java is valid to put the symbols in front of the type(String for this case) or in front of the name of the variable(args). If you run your program from a terminal with a command like "java myprogram argument1 argument2" inside the method main the value of args will be args = ["argument1", "argument2"]
Upvotes: 0
Reputation: 13017
The args
variable is for inputs to your program. See tutorial on command-line arguments.
Upvotes: 0
Reputation: 146
In java your array declaration can be after the type or the name of the variable, so it is ok both methods to perform the same functionality.
args are used to store command line arguments.
Like
java YourClass arg1 arg2
arg1 and arg2 will be stored in args array at indexes args[0] and arg[1].
Upvotes: 0
Reputation: 22487
You are defining a method named "main" which is public
(anyone can call it), static
(it's a class method, not an instance method), and returns void
(does not return anything), and takes a parameter named args
that is a String
array.
In Java you can declare a String array as:
String[] args
or
String args[]
Upvotes: 12
Reputation: 30448
You can define an array in Java in two equivalent manners (as you written):
So it doesn't really matters. The first way is a bit more common, though.
Upvotes: 3