rocket mass heater kickstarter
The moose likes Beginning Java and the fly likes String args[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String args[]" Watch "String args[]" New topic

String args[]

jack worthing

Joined: Dec 02, 2008
Posts: 18
can anybody please help me
what is the difference between

and why do we need it when we dont need to enter any command-line arguements

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11650

there is no difference - they both mean the same thing. an array of Strings named 'args'.

when you type "java MyClass", java knows to look for the MyClass object, and to run the static method name 'main' that takes a String array as it's only argument.

You certainly can write other methods names 'main' that take different (or no) parameters, but java will ALWAYS look for and run the one with the String array. You would have to call any others explicitly within your code, just like any other method.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor

Joined: Oct 27, 2005
Posts: 19905

The only difference is programmer preference. I, and many with me, prefer "String[] args" over "String args[]" because it makes it much clearer that the variable type is "array of string". With the second form, the array part is much further away from the String part.

How To Ask Questions How To Answer Questions
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
"int array[]"

is C style of array declaration, people who worked in C a lot are comfortable with C style of array declaration.

"int[] array"

is java style of array declaration.

Minal Silimkar
jack worthing

Joined: Dec 02, 2008
Posts: 18
i got it now
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Just to add few lines var-args are also permitted from java 5

is same as

My Blog SCJP 5 SCWCD 5
jack worthing

Joined: Dec 02, 2008
Posts: 18
It is sorta covered in the JavaRanch Style Guide.
subject: String args[]