aspose file tools*
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
Author

String args[]

jack worthing
Greenhorn

Joined: Dec 02, 2008
Posts: 18
hi,
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

Thankyou
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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
Greenhorn

Joined: Dec 02, 2008
Posts: 18
thankyou
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
Greenhorn

Joined: Dec 02, 2008
Posts: 18
thankyou
 
 
subject: String args[]
 
Similar Threads
simple main function doubt
basic doubt
try again
Very Basic Question ....
about var-args